问题描述
在使用的ICommand
S在XAML,WPF使用 CanExecute
的方法来启用或禁用与命令相关联的控制。但是,如果我打电话执行
从程序code?我应该先检查 CanExecute
,以确保该命令可以执行,还是应该执行
照顾这个检查我?
在换句话说,我应该这样做:
如果(someCommand.CanExecute(参数,目标))
someCommand.Execute(参数,目标);
或者只是这样的:
someCommand.Execute(参数,目标);
好风格将决定你应该做前,先检查CanExecute。这将强制执行适当的分解和实施的一致性。此外,在事件中,你永远不希望使用此命令绑定到一个按钮,它会正常工作。
When using ICommand
s in XAML, WPF uses the CanExecute
method to enable or disable controls associated with the command. But what if I am calling Execute
from procedural code? Should I first check CanExecute
to make sure that the command can execute, or should Execute
take care of this check for me?
In other words, should I do this:
if (someCommand.CanExecute(parameter, target))
someCommand.Execute(parameter, target);
Or just this:
someCommand.Execute(parameter, target);
Good style would dictate that you should do the former, check CanExecute first. This will enforce proper decomposition and a consistency in implementation. Also, in the event you ever do want to use this command bound to a button, it will work as expected.
这篇关于我应该从调用程序code执行之前检查一个ICommand的CanExecute方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!