C# Delegatecommand、relaycommand 和 routedcommand 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14180688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:57:39  来源:igfitidea点击:

Difference between Delegatecommand, relaycommand and routedcommand

c#mvvmcommand

提问by Zaz

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.

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

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; }
    }

}

采纳答案by Zev Spitz

Your FindProductCommandclass implements the ICommandinterface, which means it can be used as a WPF command. It is neither a DelegateCommandnor a RelayCommand, nor is it a RoutedCommand, which are other implementations of the ICommandinterface.

您的FindProductCommand类实现了ICommand接口,这意味着它可以用作WPF命令。它既不是 aDelegateCommand也不是 a RelayCommand,也不是 a RoutedCommand,它们是ICommand接口的其他实现。



FindProductCommandvs DelegateCommand/RelayCommand

FindProductCommand对比DelegateCommand/RelayCommand

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

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

For example, instead of your entire class, you could write:

例如,你可以这样写,而不是整个班级:

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

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

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

Some implementations of DelegateCommand/ RelayCommand:

DelegateCommand/ 的一些实现RelayCommand

Related:

有关的:



FindProductCommandvs RoutedCommand

FindProductCommand对比 RoutedCommand

Your FindProductCommandwill execute FindProductwhen triggered.

FindProductCommandFindProduct在触发时执行。

WPF's built-in RoutedCommanddoes something else: it raises a routed eventwhich 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 RoutedCommanditself specifically to one or more objects that trigger the command, e.g. a button, a menu item, or a context menu item.

WPF 的内置功能RoutedCommand还有其他功能:它引发一个路由事件,该事件可由可视化树中的其他对象处理。这意味着您可以将命令绑定附加到要执行的其他对象FindProduct,同时将其RoutedCommand本身专门附加到触发命令的一个或多个对象,例如按钮、菜单项或上下文菜单项。

Some related SO answers:

一些相关的SO答案: