ICommand 实施问题

发布于 2024-08-19 01:33:19 字数 806 浏览 1 评论 0原文

我正在执行以下操作 教程,了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在你怀里撒娇 2024-08-26 01:33:19

_canExecute 将是一个 Predicate,而 _execute 将是一个 Action

请参阅我的代理命令博客文章了解另一个示例。

_canExecute would be a Predicate<object>, whereas _execute would be an Action<object>.

See my delegate command blog post for another example.

携君以终年 2024-08-26 01:33:19

我可能是错的,但据我所知 ICommand 通常是如何实现的并且我可以理解教程,有一个错误,它应该读取,

public bool CanExecute(object parameter) {
   return _execute == null ? false : true;
}

或者也许 _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

public bool CanExecute(object parameter) {
   return _execute == null ? false : true;
}

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.

探春 2024-08-26 01:33:19

我认为代码编写者希望通过 Predicate 委托解耦 CanExecute 逻辑,以便 Base Command 类的继承者可以决定是否像这样 CanExecute

class DefaultCommand:BaseCommand
{
    //_canExecute is supposed protected Predicate<string> in base class
    public DefaultCommand()
    {
       base._canExecute =x=>x=="SomeExecutable";
    }
}

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

class DefaultCommand:BaseCommand
{
    //_canExecute is supposed protected Predicate<string> in base class
    public DefaultCommand()
    {
       base._canExecute =x=>x=="SomeExecutable";
    }
}
时光暖心i 2024-08-26 01:33:19

默认假设是 CanExecute 始终为 true,除非已传递 bool 委托来进行评估。

The default assumption is that CanExecute is always true unless a bool delegate has been passed to assess otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文