跟据网上前辈们的资料。了解到命令在MVVM绑定有三种行式。
1.DelegateCommand
2.RelayCommand
3.AttachbehaviorCommand
1 /// <summary> 2 /// Delegatecommand,继承一个命令接口。 3 /// </summary> 4 public class DelegateCommand : ICommand 5 { 6 Func<object, bool> canExecute; 7 Action<object> executeAction; 8 bool canExecuteCache; 9 10 public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute) 11 { 12 this.executeAction = executeAction; 13 this.canExecute = canExecute; 14 } 15 16 #region ICommand Members 17 18 public bool CanExecute(object parameter) 19 { 20 bool temp = canExecute(parameter); 21 22 if (canExecuteCache != temp) 23 { 24 canExecuteCache = temp; 25 if (CanExecuteChanged != null) 26 { 27 CanExecuteChanged(this, new EventArgs()); 28 } 29 } 30 31 return canExecuteCache; 32 } 33 34 public event EventHandler CanExecuteChanged; 35 36 public void Execute(object parameter) 37 { 38 executeAction(parameter); 39 } 40 41 #endregion 42 }
在代码中不难看出这两个变量。Func<object, bool> canExecute; Action<object> executeAction; 具体这两种变量的含义网上有很多。可以百度。
时间: 2024-09-29 16:51:33