Caliburn.Micro学习笔记(一)----引导类和命名匹配规则

简介:

Caliburn.Micro学习笔记目录

用了几天时间看了一下开源框架Caliburn.Micro

这是他源码的地址http://caliburnmicro.codeplex.com/

文档也写的很详细,自己在看它的文档和代码时写了一些demo和笔记,还有它实现的原理记录一下

学习Caliburn.Micro要有MEF和MVVM的基础

先说一下他的命名规则和引导类

以后我会把Caliburn.Micro的

Actions

IResult,IHandle

IConductor  ,Conductor<T>

这些常用功能写下来。

先看一下Caliburn.Micro的大概流程,画的不太好,先这样吧

好了,我们开始今天的笔记。

从一个小例子说起  Demo下载:BootstrapperAndConventions.rar

这个例子是有父窗体打开一下子窗体的小功能

 

 

程序要引入的三个类库

Caliburn.Micro

System.Windows.Interactivity

System.ComponentModel.Composition

上边两个Caliburn.Micro的例子里有提供下边的在Vs里就能找到

看一下引导类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public  interface  IShell
    {
 
    }
    public  class  MyBootstrapper:Bootstrapper<IShell>
    {
 
        private  CompositionContainer _container;
 
        //用MEF组合部件
        protected  override  void  Configure()
        {
            _container =  new  CompositionContainer(
                new  AggregateCatalog(AssemblySource.Instance.Select(x =>  new  AssemblyCatalog(x)).OfType<ComposablePartCatalog>()));
 
            ///如果还有自己的部件都加在这个地方
            CompositionBatch _batch =  new  CompositionBatch();
            _batch.AddExportedValue<IWindowManager>( new  WindowManager());
            _batch.AddExportedValue<IEventAggregator>( new  EventAggregator());
            _batch.AddExportedValue(_container);
 
 
            _container.Compose(_batch);
        }
        //根据传过来的key或名称得到实例
        protected  override  object  GetInstance(Type service,  string  key)
        {
            string  _contract =  string .IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(service) : key;
 
            var  _exports = _container.GetExportedValues< object >(_contract);
 
            if  (_exports.Any())
            {
                return  _exports.First();
            }
            throw  new  Exception( string .Format( "找不到{0}实例" , _contract));
        }
        //获取某一特定类型的所有实例
        protected  override  IEnumerable< object > GetAllInstances(Type service)
        {
            return  _container.GetExportedValues< object >(AttributedModelServices.GetContractName(service));
        }
        //将实例传递给 Ioc 容器,使依赖关系注入
        protected  override  void  BuildUp( object  instance)
        {
            _container.SatisfyImportsOnce(instance);
        }
 
    }

 我们要实现Bootstrapper<T>这个类

一般我用我MEF做为容器,重写这个类的三个方法,写法也比较固定,就像上边我写的那这样

如果有自己的一些东西需要配置可以写在Config里

除了上边的三个方法还有OnStartup和OnExit分别是程序进入和退出的执行事件,可根据自己的需要做相应的重写

还要在App.xaml里加入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Application x:Class= "CalibrunMicAction.App"
              xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:booter= "clr-namespace:CalibrunMicAction" >
     <Application.Resources>
         <ResourceDictionary>
             <ResourceDictionary.MergedDictionaries>
                 <ResourceDictionary>
                     <booter:Mybootstrapper x:Key= "appbooter" />
                 </ResourceDictionary>
             </ResourceDictionary.MergedDictionaries>
         </ResourceDictionary>
     </Application.Resources>
</Application>

 

这样程序 就会打开Export IShell的窗体

原理

是根据反射有MEF 去查找容器里是否有Exprort  IShell的ViewModel如果有就根据名称去匹配相应的View映射关系并打开,

如果没有找到就抛出异常

 

1
2
3
4
5
6
7
8
9
<Window x:Class= "WpfApplication1.MyMainView"
         xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml"
         Title= "MyMainView"  Height= "300"  Width= "300" >
     <StackPanel>
         <TextBlock x:Name= "StrMain"  FontSize= "50" />
         <Button x:Name= "OpenOneChild"  Content= "OpenAWindow"  Width= "120"  Height= "30" />
     </StackPanel>
</Window>

 

 MainViewModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using  Caliburn.Micro;
using  System;
using  System.Collections.Generic;
using  System.ComponentModel.Composition;
using  System.Linq;
using  System.Text;
 
namespace  WpfApplication1
{
     [Export( typeof (IShell))]
     public  class  MyMainViewModel
     {
         readonly  IWindowManager _myWM;
         public  string  StrMain
         {
             get ;
             private  set ;
         }
         [ImportingConstructor]
         public  MyMainViewModel(IWindowManager wm)
         {
             StrMain =  "Main!!!!!!" ;
             _myWM = wm;
         }
         MyChildOneViewModel _MyChildW =  new  MyChildOneViewModel();
         public  void  OpenOneChild()
         {
             
             _myWM.ShowDialog(_MyChildW);
         }
     }
}

 

你会发现MainView的后台代码和前台都没有指定ViewModel

这是Caliburn.Microj里很棒的一点命名匹配规则原理:它用利用反射和正则表达式去匹配View和ViewModel

系统现有的是自动匹配名称为View和ViewModel  、PageView和PageViewModel结尾的窗体和类

如果想自己定义一种匹配规则也是可以的,我这就就不讲了

运行起来你会发现

TextBlock和Button的属性和事件也自动匹配上了

原理:

匹配好View和ViewModel后

去查找View里的元素名称和viewModel里的方法或属性是否有一至的如果有一至的就绑定

 

 

!注意!:给控件命名的时候如txt_abc这样加下划线Calibrn会把这个名字分开

成txt和abc两个属性它会去txt属性里去找abc属性绑定

 

代码里打开子窗体是用的Caliburn.Micro自己的IWindowManager接口

这是一个专门用来打开窗体的类

它可以以Show() ShowDialog还有ShowPopup形式打开窗体

今天就先说到这,下次会写一下Caliburn的Actions

Demo下载:BootstrapperAndConventions.rar

 

本文转自lpxxn博客园博客,原文链接:http://www.cnblogs.com/li-peng/p/3367910.html,如需转载请自行联系原作者

相关文章
|
23天前
|
存储 算法 数据库
【C++ 软件设计思路】学习C++中如何生成唯一标识符:从UUID到自定义规则
【C++ 软件设计思路】学习C++中如何生成唯一标识符:从UUID到自定义规则
103 0
|
23天前
|
算法 开发者
【CMake 设计命名思路】自定义命令命名规范与风格指南
【CMake 设计命名思路】自定义命令命名规范与风格指南
41 0
|
编译器 Scala 开发者
类型匹配的注意事项和细节 | 学习笔记
快速学习类型匹配的注意事项和细节
58 0
|
Web App开发 PHP 索引
如何修改thinkPHP路由规则,使U方法自动生成伪静态链接
效果 假设网站域名是A.com,那么可以: 通过A.com/xwzx.html 访问A.com/Home/News/index.html?id=xwzx 或者通过A.
1197 0