参考:http://caliburn.codeplex.com/wikipage?title=Command%20Basics&referringTitle=Documentation

command在wpf的viewmodel中使用的非常的广泛,讨论的也非常多.

来看下caliburn对command的支持

caliburn支持三种方式command的注册

  • 容器
  • 资源
  • 绑定

Command解析字符串

parser.RegisterMessageParser("ResourceCommand", new CommandMessageParser(binder, CommandSource.Resource));
parser.RegisterMessageParser("ContainerCommand", new CommandMessageParser(binder, CommandSource.Container));
parser.RegisterMessageParser("BoundCommand", new CommandMessageParser(binder, CommandSource.Bound));

为简化xaml,在Attach中采用了特定解析的字符串,下面可以看到

1.使用容器

首先定义一个对象

注意点:

  1. 挂上Command标签
  2. 方法必须为Execute和CanExecute
[Command]
public class ShowMessageCommand
{//Note: A command must have an entry point.  A method named 'Execute' is searched for by default.  //Note: Use the CommandAttribute.ExecuteMethod to specify a different method.//Note: The 'Execute' method inherits all the features available to actions.public void Execute(string message){MessageBox.Show(message);}//NOTE:  This is picked up automatically by the ActionFactory based on its naming convention.public bool CanExecute(string message){return !string.IsNullOrEmpty(message);}
}

xaml

<Button Content="Attached Container Command w/ 1 Parameter" cal:Message.Attach="ContainerCommand ShowMessage(message.Text)"/>

注意xaml使用方式,ContainerCommand为特定的解析字符串ShowMessage则代表ShowMessageCommand,以下规则是一样的

2.使用资源

首先然后定义对象,注意Preview标签

public class ShowTitledMessageCommand
{//Note: A command must have a method named 'Execute'//Note: The 'Execute' method inherits all the features available to actions.[Preview("CanExecute")]public void Execute(string title, string message){MessageBox.Show(message, title);}public bool CanExecute(string title, string message){return !string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(message);}
}

定义资源

<Window.Resources><!--Note: Adding a command to resources.--><local:ShowTitledMessageCommand x:Key="ShowTitledMessage" />
</Window.Resources>

xaml引用

<Button Content="Attached Resource Command w/ 2 Parameters"cal:Message.Attach="ResourceCommand ShowTitledMessage(title.Text, message.Text)" />

3.使用绑定

要绑定的前提是需要有源

首先要定义源

<Window.DataContext><!--Note: Makes the presentation model available for binding through the data context.--><local:MyModel />
</Window.DataContext>

使用BoundCommand绑定

<TextBox x:Name="message" /><!--Note: Executes the command located through data binding.  The framework infers the trigger type.-->
<Button Content="Click Me!"cal:Message.Attach="BoundCommand TheCommand(message.Text)" />

我们通过代码来看下不同的解析过程

        protected override void SetCore(CommandMessage message, DependencyObject target, string coreOfMessage){switch(_commandSource){case CommandSource.Resource:var frameworkElement = target as FrameworkElement;if(frameworkElement != null){frameworkElement.Loaded +=delegate { message.Command = frameworkElement.GetResource<object>(coreOfMessage); };}
                    break;case CommandSource.Container:message.Command = ServiceLocator.Current.GetInstance(null, coreOfMessage);break;case CommandSource.Bound:var binding = new Binding(coreOfMessage);BindingOperations.SetBinding(message, CommandMessage.CommandProperty, binding);
                    break;default:throw new NotSupportedException(_commandSource + " is not a supported command source.");}}

实质上是非常简单的,主要手段还是通过给CommandMessage的Command赋值,方式不同而已.以上三种方式一比较,第一种是最简单的,这也是依赖注入带来的好处,不需要事先显示声明对象.效果都是一样的

来看下原始的用法

<Button Content="Triggers: Container Command With 1 Explicit Parameters"><cal:Message.Triggers><cal:RoutedMessageTriggerCollection><cal:EventMessageTrigger EventName="Click"><cal:EventMessageTrigger.Message><!--Note ResolveExtension doesn't exist in Silverlight, but you can auto resolve by string key. See SL samples.--><cal:CommandMessage Command="{cal:Resolve Key=ShowMessage}"><!--Note: The declaration of parameters is different from Silverlight.--><cal:Parameter Value="{Binding ElementName=message, Path=Text}"/></cal:CommandMessage></cal:EventMessageTrigger.Message></cal:EventMessageTrigger></cal:RoutedMessageTriggerCollection></cal:Message.Triggers>
</Button>

转载于:https://www.cnblogs.com/Clingingboy/archive/2009/12/29/1634712.html

Caliburn笔记-基本Command(wpf框架)相关推荐

  1. Caliburn笔记-Action简写(wpf框架)

    http://caliburn.codeplex.com/wikipage?title=Action%20Basics&referringTitle=Documentation public ...

  2. WPF框架教程 | 从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器

    之前时间一直在使用Caliburn.Micro这种应用了MVVM模式的WPF框架做开发,是时候总结一下了. Caliburn.Micro(https://blog.csdn.net/lzuacm/ar ...

  3. Python 网络爬虫笔记9 -- Scrapy爬虫框架

    Python 网络爬虫笔记9 – Scrapy爬虫框架 Python 网络爬虫系列笔记是笔者在学习嵩天老师的<Python网络爬虫与信息提取>课程及笔者实践网络爬虫的笔记. 课程链接:Py ...

  4. WPF框架嵌套用户控件,显示与切换(详细,代码复制可用)

    WPF框架手敲实现嵌套页面,点击显示与切换 下面附上成果图吧,看了之后觉得适用就可以参考一下,否则就不要浪费时间了 操作时间 1.此篇作为学习记录,是一个WPF框架window嵌套用户控件的使用.点击 ...

  5. WPF框架的内存泄漏BUG

    用户在使用GIX4某模块的过程中,内存只见加不见减.我们怀疑出现了内存泄漏,所以我花了相当一段时间来进行此问题的排查. 我使用Red Gate公司的产品ANTS Memory Profiler 5进行 ...

  6. WPF 框架全构建环境虚拟机硬盘分享

    现在 WPF 完全开源了,咱可以构建自己私有的版本.我分享一个虚拟机硬盘给你,只要你下载下来,通过 VMWare 导入,即可无需任何配置,拿到一个能构建 WPF 官方源代码的全构建环境.可以用来只做你 ...

  7. WPF笔记(1.1 WPF基础)——Hello,WPF!

    WPF笔记(1.1 WPF基础)--Hello,WPF! 原文:WPF笔记(1.1 WPF基础)--Hello,WPF! Example 1-1. Minimal C# WPF application ...

  8. 设计模式学习笔记——命令(Command)模式

    设计模式学习笔记--命令(Command)模式 @(设计模式)[设计模式, 命令模式, command] 设计模式学习笔记命令Command模式 基本介绍 命令案例 类图 实现代码 Command接口 ...

  9. 设计模式学习笔记——状态(State)模式框架

    设计模式学习笔记--状态(State)模式框架 @(设计模式)[设计模式, 状态模式, State] 设计模式学习笔记状态State模式框架 基本介绍 状态案例 类图 实现代码 State接口 Day ...

最新文章

  1. 关于EF中ApplyCurrentValues和ApplyOriginalValues区别
  2. svn 回归某一个特定版本
  3. mysql修改root用户的密码
  4. Hibernate 中集合对象的抓取策略(Fetching strategies)
  5. jquery ajax返回html乱码解决
  6. 盛夏光年——14年暑期总结
  7. 对象存储 OSS > 开发指南 > 存储类型 > 存储类型介绍
  8. 【C、C++】ctype.h、cctype中的isnumber()函数和isdigit()函数的区别
  9. [转载] Python中Numpy包的用法
  10. idea 创建项目并同步到git仓库
  11. java ssm旅游网站系统源码jsp maven项目推荐
  12. 支付宝小程序使用阿里图标
  13. unix下的softlink和hardlink
  14. 使用NOKIA MMS LIBRARY发送中国移动彩信
  15. 机器认知、人机交互、边缘计算……在这里,他们谈论了关于AI的关键议题
  16. 【微信公众号】7、SpringBoot整合WxJava新增临时、永久素材
  17. 八评腾讯:解密腾讯的中年危机
  18. 从与迪思杰签约 看浪潮主机生态如何布局?
  19. SpringCloud Alibaba Senta处理分布式事务
  20. ftp下载怎么操作,使用教程

热门文章

  1. Linux之虚拟机里的REHL7的IP
  2. ChartType属性
  3. ECMAScript5之Object学习笔记(二)
  4. 2008秋-计算机软件基础- 实验一 参考源程序
  5. webpack结合reactjs、vuejs项目中图片处理
  6. 19.12 添加自定义监控项目 19.13/19.14 配置邮件告警 19.15 测试告警 19.16 不发邮件的问题处理...
  7. OpenStack collectd的从零安装服务端
  8. Spring Task配置
  9. div+css 和 xhtml+css是一回事么?
  10. 横瓜执导众程序员开展大讨论关于C、JAVA及其它主流IT技术使用情况和优点缺点。...