CanExecuteChanged 的用途是什么?
我可以使用 CanExecuteChanged 更改“可以执行”条件吗?
或者...它的用途是“做什么”?
Can I use CanExecuteChanged to change the "can execute" condition?
Or else... "for what" its used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,您不能使用它来更改可以执行状态。它是一个事件,参与 ICommand 模式的对象可以选择监听此事件,例如按钮可以使用此事件来知道何时重新查询命令状态(通过调用 canexecute 方法) ) 设置其启用状态。
为了使可以执行模式发挥作用,需要有一些东西可以用来引发事件。 Prism 的
DelegateCommand
有一个方法,您可以调用该方法来手动引发此事件,因此订阅者如果选择加入该模式,将重新查询可以执行方法。示例
在以下基于 Prism 的示例中,我们在执行保存命令时将 SaveCommand
CanExecute
的状态从 false 更改为 true。对RaiseCanExecuteChanged
的调用将导致引发CanExecuteChanged
事件,并导致客户端调用CanExecute
方法。实际上,这将使绑定到 SaveCommand 的“保存”按钮将其状态从启用更改为禁用,然后再次更改回启用。No you can not use it to change the can execute state. It is an event and objects which participate in the
ICommand
pattern can choose to listen to this event e.g. a button may use this event to know when to re-query the commands state (by calling the can execute method) to set its enabled state.In order for the can execute pattern to be useful there needs to be something that can be used to raise the event. Prism's
DelegateCommand
has a method you can call to manually raise this event so subscribers will re-query the can execute method if they have opted into the pattern.Example
In the following Prism based example we change the state of SaveCommand
CanExecute
from false to true whilst the save command is executing. The call toRaiseCanExecuteChanged
will cause theCanExecuteChanged
event to be raised, and clients to call theCanExecute
method. In practice this would make a Save button that was bound toSaveCommand
change its state from enabled to disabled and back to enabled again.或者只需调用 System.Windows.Input.CommandManager.InvalidateRequerySuggested() 以便重新评估您的 CanExecute 处理程序。
Or just call
System.Windows.Input.CommandManager.InvalidateRequerySuggested()
so that yourCanExecute
handlers are re-evaluated.我不知道这对性能有何影响;然而,无论如何,它们似乎都不太伟大。这就是我用的。
I don't know what the performance implications are for this; however, they don't seem to be too great, either way. This is what I use.