[WPF] Caliburn Micro学习三 Binding
Caliburn Micro学习一 Installation
Caliburn Micro学习二 Infrastructure
如果说WPF推崇的Binding开辟了一条UI开发新的方式——让写代码的人专注在代码上,让界面设计师去实现界面,他们工作在同一个项目上,使用不同的IDE,最终Build出来真正的产品,那Caliburn Micro无疑是一个加速器,它不仅加速了Binding的使用范畴,还加速了代码的编写速度。
首先,得介绍另外一个Assembly:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
这个Assembly包含在Express Blend SDK里面,Microsoft Download Page:http://www.microsoft.com/en-us/download/details.aspx?id=3062
没有Caliburn.Micro,绑定到一个Event到一个ICommand
public ICommand CommandDataPathSelectionChanged
{
get
{
if (_cmd_path_selchged == null)
_cmd_path_selchged = new ACRelayCommand(
x => OnDataPathSelectionChanged(x)
); return _cmd_path_selchged;
}
}
其中,ACRelayCommand的定义:
public class ACRelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields #region Constructors
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public ACRelayCommand(Action<object> execute)
: this(execute, null)
{
} /// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public ACRelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute"); _execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors #region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
} public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
} public void Execute(object parameter)
{
_execute(parameter);
}
#endregion // ICommand Members
}
而作为对比,使用了Caliburn.Micro,可以直接绑定Event到一个Method
<ComboBox Name="cmbDataPath" Width="485" ItemsSource="{Binding DataPathList}"
SelectedItem="{Binding SelectedDataPath}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cal:ActionMessage MethodName="OnSelectionChanged">
<cal:Parameter Value="$source" />
<cal:Parameter Value="$eventArgs" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
其中,Parameter的Value可以指定如下值:
- $eventArgs – EventArgs对象.
- $dataContext – UI界面空间的DataContext.
- $source – 实际FrameworkElement.
- $view - 绑定在ViewModel的View.
- $executionContext - 执行上下文.
- $this - UIelement 对象.
另外,常用的XAML中绑定语法:
<Button Content="DoSomething"
cal:Message.Attach="[Event Click] = [Action DoSomething($dataContext)]" />
甚至同时绑定多个Event:
<Button Content="Let's Talk"
cal:Message.Attach="[Event MouseEnter] = [Action Talk('Hello', Name.Text)]; [Event MouseLeave] = [Action Talk('Goodbye', Name.Text)]" />
简化的语法:
<Button Content="Click Me"
cal:Message.Attach="SayHello(Name)" />
[WPF] Caliburn Micro学习三 Binding的更多相关文章
- [WPF] Caliburn Micro学习二 Infrastructure
Caliburn Micro学习一 Installation http://blog.csdn.net/alvachien/article/details/12985415 Step 1. 无论是通过 ...
- [WPF] Caliburn Micro学习一 Installation
在之前的文章(http://blog.csdn.net/alvachien/article/details/5670838)里面,已经提到过MVC和MVVM在Design Pattern上的比较. 首 ...
- Caliburn.Micro学习笔记(三)----事件聚合IEventAggregator和 Ihandle<T>
Caliburn.Micro学习笔记目录 今天 说一下Caliburn.Micro的IEventAggregator和IHandle<T>分成两篇去讲这一篇写一个简单的例子 看一它的的实现 ...
- Caliburn.Micro学习笔记(二)----Actions
Caliburn.Micro学习笔记目录 上一篇已经简单说了一下引导类和简单的控件绑定 我的上一个例子里的button自动匹配到ViewModel事件你一定感觉很好玩吧 今天说一下它的Actions, ...
- Caliburn.Micro学习笔记目录
Caliburn.Micro学习笔记(一)----引导类和命名匹配规则 Caliburn.Micro学习笔记(二)----Actions Caliburn.Micro学习笔记(三)----事件聚合IE ...
- Caliburn.Micro学习笔记(一)----引导类和命名匹配规则
Caliburn.Micro学习笔记目录 用了几天时间看了一下开源框架Caliburn.Micro 这是他源码的地址http://caliburnmicro.codeplex.com/ 文档也写的很详 ...
- Caliburn.Micro学习笔记(四)----IHandle<T>实现多语言功能
Caliburn.Micro学习笔记目录 说一下IHandle<T>实现多语言功能 因为Caliburn.Micro是基于MvvM的UI与codebehind分离, binding可以是双 ...
- Caliburn.Micro学习笔记目录——li-peng
Caliburn.Micro学习笔记(一)----引导类和命名匹配规则 Caliburn.Micro学习笔记(二)----Actions Caliburn.Micro学习笔记(三)----事件聚合IE ...
- Caliburn.Micro学习笔记(五)----协同IResult
Caliburn.Micro学习笔记目录 今天说一下协同IResult 看一下IResult接口 /// <summary> /// Allows custom code to execu ...
随机推荐
- Flask中的cookie和session
from flask import Flask app = Flask(__name__) 一.cookie from flask import Flask, make_response, reque ...
- strcpy()、strncpy()和memcpy()对比
strcpy()函数声明:char *strcpy(char *dest, const char *src)返回参数:指向最终的目标字符串 dest 的指针.注意事项:只能复制char类型的字符数组, ...
- window 下Notepad++设置为文本文件的默认打开程序失败
1.右键Notepad++的可执行程序,选择"属性" -- "兼容性" , 设置Notepad++以管理员的身份运行 2.打开Notepad++ ," ...
- [Luogu4550] 收集邮票
题目描述 有n种不同的邮票,皮皮想收集所有种类的邮票.唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是n种邮票中的哪一种是等概率的,概率均为1/n.但是由于凡凡也很喜欢邮票,所 ...
- OSI七层模型和五层TCP/IP协议
1.查公网ip的方法: windows,打开浏览器,访问百度,搜IP即可 linux:curl ifconfig.me 2.OSI七层模型 ==网络工程师:== 物理层 1层,通信介质的信号到数字信号 ...
- Js极客之路 - 简化操作
1.对字符串使用单引号(避免动态添加元素时html会出现的双引号"引起的冲突,方便操作 - 单引号一个键,双引号两个键) // bad var name = "Barrior&qu ...
- Spring 源码阅读之 深入理解 finishBeanFactoryInitialization
源码入口 上篇博文中我们看到了将Spring环境中的 BeanPostProcessor找出来,添加到BeanFactory中的beanPostProcessors中,统一维护,本片博文继续往下拓展, ...
- 【JS】深入理解JS原型和继承
前言 在学习JS中的原型,原型链,继承这些知识之前,我们先学习下基础知识:函数和对象的关系. 我们一直都知道,函数也是对象的一种,因为通过instanceof就可以判断出来.但是函数和对象的关系并不 ...
- 模块基础 day15
目录 模块的四种形式 内置模块 pip安装的模块 自定义模块 包(模块) import和from···import 循环导入 模块的搜索路径 python文件的两种用途 模块的四种形式 模块就是一系列 ...
- Java多线程(十五):CountDownLatch,Semaphore,Exchanger,CyclicBarrier,Callable和Future
CountDownLatch CountDownLatch用来使一个线程或多个线程等待到其他线程完成.CountDownLatch有个初始值count,await方法会阻塞线程,直到通过countDo ...