wpf ICommand 与 RoutedCommand
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1135983/
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
ICommand vs RoutedCommand
提问by PaN1C_Showt1Me
Let's have a button Command
property bound to a custom command.
让我们将一个按钮Command
属性绑定到一个自定义命令。
When should I implement ICommand
and when derive from RoutedCommand
? I see that RoutedCommand implements ICommand.
我应该什么时候实施ICommand
,什么时候派生自RoutedCommand
?我看到RoutedCommand 实现了 ICommand。
In which case could I need to implement an ICommand
?
What about MVVM model? Which one suits better for this purpose?
在哪种情况下,我需要实施ICommand
? MVVM模型呢?哪一个更适合这个目的?
回答by Richard McGuire
As you have noticed the RoutedCommand
classis an implementation of the ICommand
interface, its main distinction if that its function is similar to that of a RoutedEvent
:
正如您所注意到的,RoutedCommand
该类是ICommand
接口的一个实现,它的主要区别在于其功能类似于 a 的功能RoutedEvent
:
The Execute and CanExecute methods on a RoutedCommand do not contain the application logic for the command as is the case with a typical ICommand, but rather, these methods raise events that traverse the element tree looking for an object with a CommandBinding. The event handlers attached to the CommandBinding contain the command logic.
The Execute method raises the PreviewExecuted and Executed events. The CanExecute method raises the PreviewCanExecute and CanExecute events.
RoutedCommand 上的 Execute 和 CanExecute 方法不像典型的 ICommand 那样包含命令的应用程序逻辑,相反,这些方法引发了遍历元素树以查找具有 CommandBinding 的对象的事件。附加到 CommandBinding 的事件处理程序包含命令逻辑。
Execute 方法引发 PreviewExecuted 和 Executed 事件。CanExecute 方法引发 PreviewCanExecute 和 CanExecute 事件。
In a case when you don't want the behavior of the RoutedCommand
you'll be looking at your own implementation of ICommand
. As for the MVVM pattern I can't say that one solution, it seems that everyone has their own methodology. However, here are a few approaches to this problem that I've come across:
如果您不想要 的行为,RoutedCommand
您将查看自己的ICommand
. 至于MVVM模式我不能说是一种解决方案,似乎每个人都有自己的方法论。但是,这里有一些我遇到的解决这个问题的方法:
- Using RoutedCommands with a ViewModel in WPF
- Relaying Command Logic
- Simple Command(almost identical to Relay Command but worth reading)
- 在 WPF 中将 RoutedCommands 与 ViewModel 一起使用
- 中继指令逻辑
- 简单命令(几乎与中继命令相同,但值得一读)
回答by micahtan
The only thing I would add to Rich McGuire's answer is that RoutedCommands (and their more prevalent descendant RoutedUICommandhave to be wired up with event handlers to work correctly.
我要添加到 Rich McGuire 的答案中的唯一一件事是 RoutedCommands(及其更流行的后代RoutedUICommand必须与事件处理程序连接才能正常工作。
Most MVVM implementations I have come across attempt to leverage binding against the ViewModel and thus the ViewModel (and not the View) owns the CanExecute/Execute logic.
我遇到的大多数 MVVM 实现都试图利用对 ViewModel 的绑定,因此 ViewModel(而不是 View)拥有 CanExecute/Execute 逻辑。
In contrast, the event handlers move that burden to the View. The handling can then be propagated to the ViewModel, but this means a slightly higher degree of coupling between ViewModel and View (casting + method call, etc.).
相比之下,事件处理程序将负担转移到视图。然后可以将处理传播到 ViewModel,但这意味着 ViewModel 和 View 之间的耦合程度略高(强制转换 + 方法调用等)。