该ICommand接口还定义了一个CanExecute方法。您可以在执行开始时使该命令返回false,并在执行完成时将其设置回true。这也为您提供了在命令执行期间禁用按钮的好处。
我不使用RelayCommand,所以我不确定它是否与 ' 方法等效,DelegateCommand但是RaiseCanExecuteChanged使用DelegateCommand(本质上与RelayCommand安全的):
SaveCommand = new DelegateCommand<CommandParam>(MyCommandExecute, MyCommandCanExecute);
private bool canExecute;
private bool MyCommandCanExecute()
{
return canExecute;
}
private void MyCommandExecute(CommandParam parm)
{
// Change the "can execute" status and inform the UI.
canExecute = false;
SaveCommand.RaiseCanExecuteChanged();
DoStuff();
// Change the "can execute" status and inform the UI.
canExecute = true;
SaveCommand.RaiseCanExecuteChanged();
}