开源框架Caliburn.Micro

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

 

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

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

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

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

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

以后我会把Caliburn.Micro的

Actions

IResult,IHandle

IConductor  ,Conductor<T>

这些常用功能写下来。

从一个小例子说起  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分别是程序进入和退出的执行事件,可根据自己的需要做相应的重写

下边看一下MainView和MainViewModel

1
2
3
4
5
6
7
8
9
<Window x:Class="WpfApplication1.MyMainView"
        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的属性和事件也自动匹配上了

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

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

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

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

Demo下载:BootstrapperAndConventions.rar

原文地址:https://www.cnblogs.com/Leo_wl/p/3368174.html