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

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

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

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

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

    还要在App.xaml里加入

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

    MainViewModel

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

    你会发现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

    作者:李鹏
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    ajax专题
    luogu P1346 电车 最短路
    luogu P1462 通往奥格瑞玛的道路 最短路
    luogu P1328 生活大爆炸版石头剪刀布
    luogu P1315 联合权值 枚举
    luogu P1156 垃圾陷阱 背包问题
    luogu P1217 回文质数 枚举
    luogu P3650 滑雪课程设计 枚举
    luogu1209 修理牛棚 贪心
    luogu P1223 排队接水 贪心
  • 原文地址:https://www.cnblogs.com/rogerschong/p/4157526.html
Copyright © 2011-2022 走看看