无参Command:
1 internal class DelegateCommand : ICommand 2 { 3 private readonly Action _execute; 4 private readonly Func<bool> _canExecute; 5 6 public DelegateCommand(Action execute) : this(execute, null) { } 7 public DelegateCommand(Action execute, Func<bool> canExecute) 8 { 9 _execute = execute ?? throw new ArgumentNullException(nameof(execute)); 10 _canExecute = canExecute; 11 } 12 13 public void Execute(object parameter) 14 { 15 _execute(); 16 } 17 public bool CanExecute(object parameter) 18 { 19 if (_canExecute == null) return true; 20 return _canExecute(); 21 } 22 23 24 public event EventHandler CanExecuteChanged 25 { 26 add => CommandManager.RequerySuggested += value; 27 remove => CommandManager.RequerySuggested -= value; 28 } 29 }