手动执行 WPF 路由命令
从代码隐藏手动执行自定义 RoatedUICommand
时,如下所示:
MyCommands.MyCommand.Execute(parameter, target)
我是否需要先调用 CanExecute
方法,还是已经在 Execute
中完成了此操作代码>方法?
When executing a custom RoutedUICommand
manually from code-behind, like this:
MyCommands.MyCommand.Execute(parameter, target)
do I need to call the CanExecute
method first or is this already done inside the Execute
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要假设 CanExecute 将被 Execute 调用。 ICommand 的接口并不意味着它在调用 Execute 时调用 CanExecute,因此,如果它仅在 CanExecute 为 true 时执行对您很重要,只需自行检查即可。
另外,扫描 RoutedUICommand 的反编译代码,我没有看到任何地方在 Execute 中检查 CanExecute。
确定何时调用 Execute/CanExecute 实际上更多的是消费者的责任。
Do not assume that CanExecute will get called with Execute. The interface for ICommand does not imply that it calls CanExecute when Execute is called, so if it is important to you that it only execute when CanExecute is true, simply check it yourself.
Also, scanning the de-compiled code for RoutedUICommand, I don't see anywhere that checks CanExecute within Execute.
It is really more of the consumer's responsibility to determine when to call Execute/CanExecute.
如果需要,您应该手动调用 CanExecute,Execute 不会检查它!
You should call CanExecute manually if you need, Execute will not check it!
您不应该假设
CanExecute
是由Execute
方法调用的,因为没有任何东西可以强制执行该行为。 所以我认为你应该自己调用CanExecute
You shouldn't assume that
CanExecute
is called by theExecute
method, since there is nothing to enforce that behavior. So IMO you should callCanExecute
yourself