CM中实现一个比较有意思的特性,就是智能匹配。

通常使用MVVM的写法:在匹配 View和ViewModel时会使用DataContext,在匹配数据属性时使用Binding,在匹配事件命令时使用Command。

而CM通过ElementConvention 实现它们的自动匹配,只需要遵循指定的命名规则[可自定义]。由于一个控件的事件有多种(比如:Button:Click,MouseEnter等等),CM提供了最常用的事件的绑定,可根据具体需求自定义。

自动绑定演示:

在View中添加如下代码:

<Window
x:Class="Caliburn.Micro.Demo.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"> <Grid Background="White">
<TextBlock x:Name="TbMain" FontSize="50" />
<Button
x:Name="OpenOneChild"
Width="120"
Height="30"
Content="OpenOneWindow" />
</Grid> </Window>

在ViewModel中添加:

  public class ShellViewModel : Caliburn.Micro.PropertyChangedBase, IShell
{
private readonly IWindowManager windowManager; [ImportingConstructor]
public ShellViewModel(IWindowManager windowManager)
{
TbMain = "This is ShewView";
this.windowManager = windowManager;
}
private string _tbMain;
public string TbMain
{
get { return _tbMain; }
set
{
_tbMain = value;
NotifyOfPropertyChange(() => TbMain);
}
} public void OpenOneChild()
{
ChildOneViewModel oneViewModel=new ChildOneViewModel();
windowManager.ShowDialog(oneViewModel);
}

项目中新建一个ChildOneView.xaml和一个ChildOneViewModel.cs。

<Window
x:Class="Caliburn.Micro.Demo.ChildOneView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Caliburn.Micro.Demo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="ChildOneView"
Width="300"
Height="300"
mc:Ignorable="d">
<StackPanel>
<TextBlock x:Name="ChildOne" />
<TextBox Text="{Binding ChildOne}" />
<TextBox />
</StackPanel>
</Window>
public class ChildOneViewModel:Screen
{
public ChildOneViewModel()
{
ChildOne = "This is ChildOneView";
} private string _childOne;
public string ChildOne
{
get { return _childOne; }
set
{
_childOne = value;
NotifyOfPropertyChange(()=>ChildOne);
}
} }

运行:

默认情况下,CM的Convention是默认开启的,可使用ViewModelBinder.ApplyConventionsByDefault = false;来关闭,或者直接使用通常写法,会自动覆盖Convention的自动绑定。

有些时候,在碰到一个属性绑定多个控件等问题时,用起来就不那么顺手了。所以平时也不太会使用此特性。

后面准备介绍一下数据绑定(Binding)和事件响应(Command)两大块的实现方式。

源码文件:http://pan.baidu.com/s/1gfHyhQN

最新文章

  1. oracle数据查询
  2. Linux环境部署(JDK/Tomcat/MySQL/证书)
  3. 8 步搭建 Node.js + MongoDB 项目的自动化持续集成
  4. jqgrid动态显示/隐藏某一列
  5. 剑指offer系列33-----把二叉树打印成多行
  6. Team Foundation Server 2010下载安装配置方法
  7. 【完结】利用 Composer 完善自己的 PHP 框架(三)——Redis 缓存
  8. 剑指offer_快速查找递增二维数组中是否存在目标
  9. Tinyxml 操作XML
  10. 从xcode 6 上传 App Store
  11. Java实现二叉树先序,中序,后序遍历
  12. Nginx+Python+uwsgi+Django的web开发环境安装及配置
  13. 如何使用Vue2做服务端渲染
  14. 易卡易APP的出现改变你的消费习惯
  15. [原]开源的视频转换器,支持gpu,绝对好用ffmpeg的GUI==》dmMediaConverter最新版本2.3
  16. html5中的网页结构
  17. HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor
  18. 最新省市区数据,sql插入语句
  19. [苹果]苹果AppStore应用审核标准
  20. Unique Binary Search Trees II leetcode java

热门文章

  1. YTU 2906: 多重继承 日期与时间
  2. 织梦首页TAG标签页的仿制
  3. c语言和oc对比
  4. Java语法基础练习2
  5. BZOJ_4177_Mike的农场_最小割
  6. gerrit使用总结
  7. hdu2089(数位DP 递推形式)
  8. Python 元组、列表
  9. python3 + selenum 环境搭建
  10. Scanner类nextLine()和next()的区别和使用方法