Delegatecommand、relaycommand 和 routedcommand 的区别 [英] Difference between Delegatecommand, relaycommand and routedcommand

查看:22
本文介绍了Delegatecommand、relaycommand 和 routedcommand 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对命令模式感到困惑.关于命令有很多不同的解释.我以为下面的代码是delegatecommand,但在阅读relaycommand之后,我有疑问.

I'm confused about command pattern. There are so many different explanations about the commands. I thought the code below was delegatecommand, but after reading about the relaycommand, I am in doubt.

relaycommand、delegatecommand 和 routedcommand 有什么区别.是否可以在与我发布的代码相关的示例中显示?

What is the difference between relaycommand, delegatecommand and routedcommand. Is it possible to show in examples that have relevance to my posted code?

class FindProductCommand : ICommand
{
    ProductViewModel _avm;

    public FindProductCommand(ProductViewModel avm)
    {
        _avm = avm;
    }

    public bool CanExecute(object parameter)
    {
        return _avm.CanFindProduct();
    }

    public void Execute(object parameter)
    {
        _avm.FindProduct();
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

}

推荐答案

您的 FindProductCommand 类实现了 ICommand 接口,表示它可以用作 WPF 命令.它既不是 DelegateCommand 也不是 RelayCommand,也不是 RoutedCommand,它们是 ICommand 的其他实现界面.

Your FindProductCommand class implements the ICommand interface, which means it can be used as a WPF command. It is neither a DelegateCommand nor a RelayCommand, nor is it a RoutedCommand, which are other implementations of the ICommand interface.

FindProductCommand vs DelegateCommand/RelayCommand

FindProductCommand vs DelegateCommand/RelayCommand

通常,当 ICommand 的实现被命名为 DelegateCommandRelayCommand 时,目的是您不必编写类实现了 ICommand 接口;相反,您将必要的方法作为参数传递给 DelegateCommand/RelayCommand 构造函数.

Generally, when an implementation of ICommand is named DelegateCommand or RelayCommand, the intention is that you don't have to write a class that implements the ICommand interface; rather, you pass the necessary methods as parameters to the DelegateCommand / RelayCommand constructor.

例如,您可以编写:

ProductViewModel _avm;
var FindPoductCommand = new DelegateCommand<object>(
    parameter => _avm.FindProduct(),
    parameter => _avm.CanFindProduct()
);

(另一个可能比节省样板代码更大的好处——如果您在视图模型中实例化 DelegateCommand/RelayCommand,您的命令可以访问内部状态那个视图模型.)

(Another, perhaps greater benefit than the savings in boilerplate code -- if you instantiate the DelegateCommand / RelayCommand within your viewmodel, your command has access to the internal state of that viewmodel.)

DelegateCommand/RelayCommand 的一些实现:

  • Microsoft Prism DelegateCommand reference
  • WPF Tutorial implementation of ICommand called DelegateCommand
  • Another implementation also called DelegateCommand
  • The original implementation of RelayCommand by Josh Smith

相关:

FindProductCommandRoutedCommand

FindProductCommand vs RoutedCommand

您的 FindProductCommand 将在触发时执行 FindProduct.

Your FindProductCommand will execute FindProduct when triggered.

WPF 的内置 RoutedCommand 做了别的事情:它引发了一个 路由事件 可以由视觉中的其他对象处理树.这意味着您可以将命令绑定附加到那些其他对象以执行 FindProduct,同时将 RoutedCommand 本身专门附加到触发命令的一个或多个对象,例如按钮、菜单项或上下文菜单项.

WPF's built-in RoutedCommand does something else: it raises a routed event which can be handled by other objects in the visual tree. This means you can attach a command binding to those other objects to execute FindProduct, while attaching the RoutedCommand itself specifically to one or more objects that trigger the command, e.g. a button, a menu item, or a context menu item.

一些相关的 SO 答案:

Some related SO answers:

这篇关于Delegatecommand、relaycommand 和 routedcommand 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆