ICommand 实施问题
我正在执行以下操作 教程,了解 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
I am doing the following tutorial, to learn about the MVVM pattern in WPF. There's something I don't understand about the following seemingly partly given implementation of ICommand interface.
In the code below the _canExecute variable is used as a method and a variable. I was thinking it's some event of some kind, but ICommand already has an event to be implemented and it's not _canExecute.
So can someone help me as to what _canExecute is supposed to be?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
_canExecute
将是一个Predicate
请参阅我的代理命令博客文章了解另一个示例。
_canExecute
would be aPredicate<object>
, whereas _execute
would be anAction<object>
.See my delegate command blog post for another example.
我可能是错的,但据我所知 ICommand 通常是如何实现的并且我可以理解教程,有一个错误,它应该读取,
或者也许 _canExecute 是一个请求转发到的函数对象。在这种情况下,教程不清楚。
无论如何,我会向作者咨询他的想法。
I may be wrong, but as far as I know how ICommand is usually implemented and I can understand the tutorial, there is a bug and it should read
or perhaps _canExecute is a function object to which a request is forwarded. In that case, the tutorial is unclear.
Anyway, I would consult with the author what he had in mind.
我认为代码编写者希望通过 Predicate 委托解耦 CanExecute 逻辑,以便 Base Command 类的继承者可以决定是否像这样 CanExecute
I think writer of the code wanted to decouple CanExecute logic via Predicate delegate so inheritors of Base Command class can decide wheter or not CanExecute like this
默认假设是 CanExecute 始终为 true,除非已传递 bool 委托来进行评估。
The default assumption is that CanExecute is always true unless a bool delegate has been passed to assess otherwise.