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.