zoukankan      html  css  js  c++  java
  • MVVM 中 ViewModelBase和 CommandBase

     1 public class ViewModelBase : INotifyPropertyChanged , IDisposable  
     2     {
     3         public virtual string DisplayName { get; protected set; }  
     4 
     5         public event PropertyChangedEventHandler PropertyChanged;
     6 
     7         protected void OnPropertyChanged([CallerMemberName]string propertyName = null)
     8         {
     9             if (PropertyChanged != null)
    10             {
    11                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    12             }
    13         }
    14 
    15         public void Dispose()
    16         {
    17             this.OnDispose();
    18         }
    19 
    20         /// <summary>  
    21         /// 若支持IDisposable,请重写此方法,当被调用Dispose时会执行此方法。  
    22         /// </summary>  
    23         protected virtual void OnDispose()
    24         {
    25 
    26         }  
    27     }

     public class CommandBase : ICommand
        {
            private readonly Action<object> _command;
            private readonly Func<object, bool> _canExecute;
    
            public CommandBase(Action<object> command, Func<object, bool> canExecute)
            {
                if (command == null)
                    throw new ArgumentNullException("command");
                _canExecute = canExecute;
                _command = command;  
            }
    
            public bool CanExecute(object parameter)
            {
                if (_canExecute == null)
                    return true;
                return _canExecute(parameter); 
            }
    
    
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }  
    
            public void Execute(object parameter)
            {
                _command(parameter);  
            }
        }
  • 相关阅读:
    开始学习C#
    关于串口数据读取的几个问题
    Joel测试
    VC查找内存泄漏技巧【转】
    思考题一
    自我介绍
    2020面向对象程序设计寒假作业1 题解
    思考题二
    题解 洛谷P2158 【[SDOI2008]仪仗队】
    深入浅出InfoPath系列
  • 原文地址:https://www.cnblogs.com/akiing/p/3444507.html
Copyright © 2011-2022 走看看