using System; using System.Windows.Input; namespace IOTContainer.MvvmBase { /// /// 命令基础类 /// public class CommandBase : ICommand { #region 字段属性 /// /// 事件 /// private Action _action; /// /// 判断可执行方法 /// private Predicate _canExecuteFunc; #endregion #region 对外方法 /// /// 初始化事件 /// /// public CommandBase(Action action) { this._action = action; } public CommandBase(Action action, Predicate canExecuteFunc) { this._action = action; this._canExecuteFunc = canExecuteFunc; } /// /// 执行事件 /// /// public void Execute(object parameter) { this._action?.Invoke(parameter); } /// /// 执行改变事件 /// public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } /// /// 执行改变 /// /// /// public bool CanExecute(object parameter) { return this._canExecuteFunc?.Invoke(parameter) != false; } #endregion } }