CanExecuteChanged 和 CommandManager.RequerySuggested 的实际任务是什么?
我从 Josh Smith 的 MVVM 教程 中获得了以下代码。
谁能快速解释一下这段代码的实际用途?
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
我不明白两件事:
CanExecuteChanged
事件是做什么的?- CommandManager.RequerySuggested 的作用是什么?
上面的代码来自此处的RelayCommand
类。
I got the following code from Josh Smith's MVVM tutorial.
Can anyone provide a quick explanation of what this code actually does?
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
I can't understand two things:
- what does the
CanExecuteChanged
event do? - what does the
CommandManager.RequerySuggested
do?
The above code is from the RelayCommand
Class from here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CanExecuteChanged
通知绑定到该ICommand
的任何命令源(如Button
或MenuItem
)返回的值由CanExecute
已更改。命令源关心这一点,因为它们通常需要相应地更新其状态(例如,如果CanExecute()
返回false
,则Button
将禁用自身) 。CommandManager
认为某些内容发生变化会影响命令执行能力时,就会引发CommandManager.RequerySuggested
事件。例如,这可能是焦点的改变。事实证明这个事件火了很多。因此,本质上,这段代码的作用是确保每当命令管理器认为命令的执行能力发生更改时,该命令都会引发
CanExecuteChanged
即使它实际上没有更改。我实际上不喜欢这种实现 ICommand.CanExecuteChanged 的方法 - 它感觉很懒而且不完全可靠。我更喜欢更细粒度的方法,其中命令公开一个方法(例如
RaiseCanExecuteChanged()
),您可以调用它来引发CanExecuteChanged
,然后在适当的位置调用它来自您的视图模型的时间。例如,如果您有一个删除当前选定客户的命令,则它会有一个
CanExecute()
处理程序,仅当选择了一个客户时才返回true
。因此,只要选定的客户发生变化,您就可以调用RaiseCanExecuteChanged
。CanExecuteChanged
notifies any command sources (like aButton
orMenuItem
) that are bound to thatICommand
that the value returned byCanExecute
has changed. Command sources care about this because they generally need to update their status accordingly (eg. aButton
will disable itself ifCanExecute()
returnsfalse
).CommandManager.RequerySuggested
event is raised whenever theCommandManager
thinks that something has changed that will affect the ability of commands to execute. This might be a change of focus, for example. Turns out that this event fires a lot.So, in essence, what this bit of code does is ensure that whenever the command manager thinks a command's ability to execute has changed, the command will raise
CanExecuteChanged
even if it hasn't actually changed.I actually dislike this approach to implementing
ICommand.CanExecuteChanged
- it feels lazy and isn't entirely reliable. I prefer a much more fine-grained approach where the command exposes a method (eg.RaiseCanExecuteChanged()
) you can call to raiseCanExecuteChanged
, then you call this at the appropriate times from your view model.For example, if you have a command that deletes the currently selected customer, it would have a
CanExecute()
handler that returnstrue
only if there is a customer selected. You would therefore callRaiseCanExecuteChanged
whenever the selected customer changes.RoatedCommands
可以自动通知它们的CanExecute
是否已更改,因为我们在这里实现ICommand
,WPF 系统不知道这一点,我们将其连线将它们发送给 CommandManager 的RequerySuggested
事件。CanExecuteChanged
。当您的按钮正在侦听此事件时,它将重新调用CanExecute
以了解最新状态。这是一篇您可能感兴趣的文章 。
RoutedCommands
can automatically notify if theirCanExecute
has changed, since we are implementingICommand
here, which the WPF system doesn't know about, we wire them to CommandManager'sRequerySuggested
event.CanExecuteChanged
is raised. As your button is listening to this event it will reinvokeCanExecute
to know the latest status.Here is an article that might be of interest.
实现 RaiseCanExecuteChanged()
命令类:
View:
ViewModel 类:
Implementing RaiseCanExecuteChanged()
Command Class:
View:
ViewModel class: