我正在学习 Mvvmlight,并且对它canExecute的 RelayCommand 感到很困惑。
基本上,我有一个Button和一个PasswordBox,view和一个Command。viewModel如果PasswordBox为空,我想要禁用Button。我的解决方案是将PasswordBox作为CommandParemeter传递给Button,然后在canExecute方法中接收PasswordBox并指定它是空还是空。我首先声明一个命令:
public ICommand CommandClear { get; private set; }
然后在以下实例化它Class Constructor:
CommandConfirm = new RelayCommand<object>((p) => ConfirmExecute(p), (p) => ConfirmCanExecute(p));
最后实现如下canExecute方法:
private bool ConfirmCanExecute(object parameter)
{
bool isExecuable = false;
var passwordBox = parameter as PasswordBox;
if (!string.IsNullOrEmpty(passwordBox.Password))
isExecuable = true;
return isExecuable;
}
上面的 canExecute 方法不起作用,因为System.Reflection.TargetInvocationException会抛出未处理的异常PresentationFramework.dll。
所以我尝试用 . 包装上面的代码try...catch。这一次,它就像魔术一样工作:
try
{
var passwordBox = parameter as PasswordBox;
if (!string.IsNullOrEmpty(passwordBox.Password))
isExecuable = true;
return isExecuable;
}
catch
{
return isExecuable;
}
我对这种行为感到很困惑canExecute,有什么想法吗?