2009-06-15 88 views
1

当从代码隐藏手动执行自定义RoutedUICommand,像这样:执行WPF手动路由命令

MyCommands.MyCommand.Execute(parameter, target) 

,我需要首先调用CanExecute方法,或者这已经是Execute方法中做了什么?

回答

10

不要以为执行CanExecute会被调用。 ICommand的接口并不意味着它在调用Execute时会调用CanExecute,所以如果它对于只在CanExecute为true时才执行很重要,那么只需自行检查它。

另外,扫描RoutedUICommand的解编译代码,我没有看到任何在Execute中检查CanExecute的地方。

确定何时调用Execute/CanExecute确实是消费者的更多责任。

1

如果您需要,您应该手动调用CanExecute,执行不会检查它!

0

您不应该认为CanExecuteExecute方法调用,因为没有什么可以强制执行该行为。所以IMO你应该自己拨打CanExecute