CAL (Prism)中的Command绑定非常好用,可以将某个button的click事件绑定到ViewModel中的某个方法上,简单明了。但是,
CAL (Prism)中的Commands对Silverlight的支持有一个限制 ,只实现了对ButtonBase的Click事件的Command Binding:
The command support in Silverlight is built using an attached behavior pattern. This pattern connects events raised by controls to code on a Presenter or PresentationModel. In WPF and Silverlight,an attached behavior is comprised of two parts: an attached property and a behavior object. The attached property establishes a relationship between the target control and the behavior object. The behavior object monitors the target control and takes action based on events or state change in the control. For example,the Composite Application Library provides an attached behavior that executes commands from the Click event of ButtonBase.
对其他控件中的事件则需要自己来实现:
Frequently,applications need to invoke commands from controls or events other than the Click event from ButtonBase . In these cases,you need to define your own attached property and behavior. The Composite Application Library provides a CommandBehaviorBase <T> to make it easier to create behaviors that interact with ICommands . This class invokes the command and monitors changes in the command's CanExecuteChanged event,and can be used to extend command support in both Silverlight and WPF.
To create your custom behavior,create a class that inherits from CommandBehaviorBase <T> and targets the control you want to monitor. In the constructor of your class,you will need to subscribe to the events you want to monitor from the control.
这个实现其是Attached Behavior 与 CAL (Prism)中Command的一个结合。这里有一个例子
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。