我正在执行以下tutorial,以了解WPF中的MVVM模式。关于下面似乎部分给定的ICommand接口的实现,有一些我不理解的地方。
在下面的代码中,_canExecute变量用作方法和变量。我认为这是某种类型的事件,但是ICommand已经有一个要实现的事件,而且它不是_canExecute。
所以有人能帮我解释一下_canExecute应该是什么样子吗?
1: #region ICommand Members
2:
3: public bool CanExecute(object parameter) {
4: return _canExecute == null ? true : _canExecute(parameter);
5: }
6:
7: public event EventHandler CanExecuteChanged {
8: add { CommandManager.RequerySuggested += value; }
9: remove { CommandManager.RequerySuggested -= value; }
10: }
11:
12: public void Execute(object parameter) {
13: _execute(parameter);
14: }
15:
16: #endregion发布于 2010-01-26 22:47:52
_canExecute将是一个Predicate<object>,而_execute将是一个Action<object>。
有关另一个示例,请参阅我的delegate command blog post。
发布于 2010-01-26 21:14:39
我可能错了,但据我所知,ICommand通常是如何实现的,并且我能够理解本教程,其中有一个bug,应该是这样的
public bool CanExecute(object parameter) {
return _execute == null ? false : true;
}或者,_canExecute可能是一个请求被转发到的函数对象。在这种情况下,教程是不清楚的。
无论如何,我会咨询作者他的想法。
发布于 2010-01-26 21:21:45
我认为代码的作者希望通过谓词委托来解耦CanExecute逻辑,这样Base Command类的继承者就可以决定是否像这样使用CanExecute
class DefaultCommand:BaseCommand
{
//_canExecute is supposed protected Predicate<string> in base class
public DefaultCommand()
{
base._canExecute =x=>x=="SomeExecutable";
}
}https://stackoverflow.com/questions/2139468
复制相似问题