WPF development time mark notebook (2) -- WVVM base class, wpfwvvm

Source: Internet
Author: User

WPF development time mark notebook (2) -- WVVM base class, wpfwvvm

INotifyPropertyChanged must be implemented when MVVM is used. It is troublesome to implement this interface every time, so the role of the Base class is embodied. The Code is as follows:

Public class ViewModelBase: INotifyPropertyChanged, IDisposable {public event PropertyChangedEventHandler PropertyChanged; public bool IsInDesignMode; // <summary> display name </summary> public virtual string DisplayName {get; protected set ;} # region construct public ViewModelBase () {}# endregion private void OnPropertyChanged (string propertyName) {if (this. propertyChanged! = Null) {this. propertyChanged (this, new PropertyChangedEventArgs (propertyName);} private static string GetProperyName (string methodName) {if (methodName. startsWith ("get _") | methodName. startsWith ("set _") | methodName. startsWith ("put _") {return methodName. substring (4);} throw new Exception (methodName + "not a method of Property");} protected void SetProperty <T> (ref T name, T value) {if (Object. equals (name, value) return; name = value; string propertyName = GetProperyName (new System. diagnostics. stackTrace (true ). getFrame (1 ). getMethod (). name); this. onPropertyChanged (propertyName);} # region IDisposable Members public void Dispose () {this. onDispose () ;}/// <summary> /// if IDisposable is supported, rewrite this method and execute this method when Dispose is called. /// </Summary> protected virtual void OnDispose () {}# endregion} public class CommandBase: System. windows. input. ICommand {private Action <Object> _ doWork; private readonly Func <object, bool> _ canExecute; /// <summary> /// instantiate a CommandBase object /// </summary> /// <param name = "doWork"> delegate a command with parameters of the object type for execution function </param> /// <param name = "canExecute"> delegates whether a command with an object-type parameter can be executed (optional) </param> // <exception c Ref = "ArgumentNullException"> the command parameter cannot be null reference </exception> public CommandBase (Action <object> doWork, Func <object, bool> canExecute = null) {if (doWork = null) throw new ArgumentNullException ("doWork"); _ canExecute = canExecute; _ doWork = doWork;} public bool CanExecute (Object parameter) {return true ;} public void Execute (Object parameter) {if (this. _ doWork! = Null) this. _ doWork (parameter);} public event EventHandler CanExecuteChanged {add {}remove {}}}

The usage is as follows, for example:

Public class TestViewModel: ViewModelBase {public TestViewModel () {this. loadCommand = new CommandBase (Object parameter) => Loading () ;}# region attribute private string _ name; /// <summary> available </summary> public string IsEnable {get {return _ name;} set {base. setProperty <string> (ref this. _ name, value) ;}# endregion # region Command // <summary> // window loading command /// </summary> public ICommand LoadCommand {get; set ;}# endregion # region method // <summary> /// initialization method // </summary> public void Loading () {}# endregion}

After reading the example, does it feel very simple? The detailed test code will be provided for your reference in subsequent articles.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.