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
Difference between Delegatecommand, relaycommand and routedcommand
提问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 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
类实现了ICommand
接口,这意味着它可以用作WPF命令。它既不是 aDelegateCommand
也不是 a RelayCommand
,也不是 a RoutedCommand
,它们是ICommand
接口的其他实现。
FindProductCommand
vs DelegateCommand
/RelayCommand
FindProductCommand
对比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.
通常,当 的实现ICommand
命名为DelegateCommand
or 时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
/ RelayCommand
within your viewmodel, your command has access to the internal state of that viewmodel.)
(另一个可能比节省样板代码更大的好处——如果您在视图模型中实例化DelegateCommand
/ RelayCommand
,您的命令可以访问该视图模型的内部状态。)
Some implementations of DelegateCommand
/ RelayCommand
:
DelegateCommand
/ 的一些实现RelayCommand
:
- Microsoft Prism DelegateCommand reference
- WPF Tutorial implementation of
ICommand
calledDelegateCommand
- Another implementationalso called
DelegateCommand
- The original implementation of
RelayCommand
by Josh Smith
- Microsoft Prism DelegateCommand 参考
ICommand
被调用的WPF教程实现DelegateCommand
- 另一种实现也称为
DelegateCommand
- 由 Josh Smith的原始实现
RelayCommand
Related:
有关的:
FindProductCommand
vs RoutedCommand
FindProductCommand
对比 RoutedCommand
Your FindProductCommand
will execute FindProduct
when triggered.
您FindProductCommand
将FindProduct
在触发时执行。
WPF's built-in RoutedCommand
does 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 RoutedCommand
itself 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答案: