MVVM之Command详谈

Command有两个功能:

a:执行一个特殊的行为:command的主要功能。

b:确定某一UIElement的视觉状态(visual state):例如确定button是否可用。

DelegateCommand:实现了ICommand,当需要使用command时,可用使用此类

a:CanExecuteChanged事件和CanExecute方法被用来确定command所施加控件的视觉状态,它们是这样工作的:当某command施加于某控件时,控件会调用CanExecute方法,来确定初始的视觉状态,假设调用者是button,如果CanExecute方法返回false,button会被禁用。button同时也会订阅CanExecuteChanged事件。当触发CanExecuteChanged事件时,会再次调用CanExecute以确定是否需要修改视觉状态。

b:Execute方法比较直白:当需要执行某些行为操作时,控件会调用它。例如,当按下按钮时。


原文地址:https://www.cnblogs.com/iwangjun/p/2415390.html