WPF ICommand所有绑定的命令都必须实现ICommand.
该接口定义了一个execute方法, 一个canExecute方法,还有一个CanExecuteChanged事件属性。
RoutedCommand是WPF中唯一继承ICommand的类,它不包含任何应用程序逻辑,只代表了一个命令。
As you have noticed the RoutedCommand class is an implementation of the ICommand interface, its main distinction if that its function is similar to that of 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.
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:
- Using RoutedCommands with a ViewModel in WPF
- Relaying Command Logic
- Simple Command (almost identical to Relay Command but worth reading)