使用Caliburn.Micro系列2:Convention
2017-06-17 14:27
232 查看
CM中实现一个比较有意思的特性,就是智能匹配。
通常使用MVVM的写法:在匹配 View和ViewModel时会使用DataContext,在匹配数据属性时使用Binding,在匹配事件命令时使用Command。
而CM通过ElementConvention 实现它们的自动匹配,只需要遵循指定的命名规则[可自定义]。由于一个控件的事件有多种(比如:Button:Click,MouseEnter等等),CM提供了最常用的事件的绑定,可根据具体需求自定义。
自动绑定演示:
在View中添加如下代码:
在ViewModel中添加:
项目中新建一个ChildOneView.xaml和一个ChildOneViewModel.cs。
View Code
运行:
默认情况下,CM的Convention是默认开启的,可使用ViewModelBinder.ApplyConventionsByDefault = false;来关闭,或者直接使用通常写法,会自动覆盖Convention的自动绑定。
有些时候,在碰到一个属性绑定多个控件等问题时,用起来就不那么顺手了。所以平时也不太会使用此特性。
后面准备介绍一下数据绑定(Binding)和事件响应(Command)两大块的实现方式。
源码文件:http://pan.baidu.com/s/1gfHyhQN
通常使用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。
public class ChildOneViewModel:Screen { public ChildOneViewModel() { ChildOne = "This is ChildOneView"; } private string _childOne; public string ChildOne { get { return _childOne; } set { _childOne = value; NotifyOfPropertyChange(()=>ChildOne); } } }
View Code
运行:
默认情况下,CM的Convention是默认开启的,可使用ViewModelBinder.ApplyConventionsByDefault = false;来关闭,或者直接使用通常写法,会自动覆盖Convention的自动绑定。
有些时候,在碰到一个属性绑定多个控件等问题时,用起来就不那么顺手了。所以平时也不太会使用此特性。
后面准备介绍一下数据绑定(Binding)和事件响应(Command)两大块的实现方式。
源码文件:http://pan.baidu.com/s/1gfHyhQN
相关文章推荐
- 使用Caliburn.Micro系列1:新建项目并引入CM
- Caliburn Micro 框架 WP8使用研究(一)简介
- Caliburn Micro 框架 WP8使用研究(三)墓碑处理
- WP7应用开发笔记(14) 使用Caliburn Micro简化MVVM
- First steps with Caliburn Micro in Windows Phone 8 系列文章
- Caliburn Micro 框架 WP8使用研究(二)页面导航
- 使用Caliburn.Micro创建简单的WPF程序
- Caliburn.Micro 应用系列(一)
- WPF 使用Caliburn.Micro 多线程打开窗口
- TC1系列双绳路程控电话交换机使用说明书
- 如何使用Lua扩展C/C++应用系列3-转自bbs.luachina.net
- 构造系列和位拷贝使用注意
- 使用JDOM操作XML系列----从数据库中读取记录集到层叠式XML文
- ASP.NET 2.0中使用webpart系列控件
- ASP系列讲座(五)使用变量和常量
- nhibernate入门系列: 使用Criteria载入对象
- PL/SQL DEVELOPER 6 模板使用系列文档上传完毕之后
- 如何使用Lua扩展C/C++应用系列1
- 如何使用Lua扩展C/C++应用系列3
- 使用PVCS系列软件构建配置管理环境(四)