using System; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows.Input; namespace HMI.UI.Entity { public class RelayCommand : ICommand { /// /// 命令能否执行 /// readonly Func _canExecute; /// /// 命令执行的方法 /// readonly Action _execute; /// /// 命令的构造函数 /// /// 命令需执行的方法 /// 命令是否可以执行的方法 public RelayCommand(Action action, Func canExecute) { _execute = action; _canExecute = canExecute; } public RelayCommand(Action action) { _execute = action; } /// /// 判断命令是否可以执行 /// /// /// public bool CanExecute(Object parameter) { if (_canExecute == null) return true; return _canExecute(); } /// /// 执行命令 /// /// public void Execute(Object parameter) { _execute((T)parameter); } /// /// 事件追加、移除 /// public event EventHandler CanExecuteChanged { add { if (_canExecute != null) CommandManager.RequerySuggested += value; } remove { if (_canExecute != null) CommandManager.RequerySuggested -= value; } } } public class NotifyBase : INotifyPropertyChanged { #region ... public virtual event PropertyChangedEventHandler PropertyChanged; protected virtual void RaisePropertyChanged(string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } protected void RaiseAndSetIfChanged(ref T old, T @new, [CallerMemberName] string propertyName = null) { old = @new; if (propertyName != null) { RaisePropertyChanged(propertyName); } } #endregion } }