zoukankan      html  css  js  c++  java
  • [Prism练习] Prism Modularity 配合 Unity Ioc 用 XML 方式

    在这个例子我想实现一个简单的demo 用来练习用配置文件的方式来加载Prism Module.

    • 假设我们要开发一个实现产品和公司资料管理的小demo,首还是建立解决方案的下的几个项目
      PrimsDemo.DesktopMain(启动项目)
      PrismDemo.DesktopCompany(公司资料管理)
      PrismDemo.DesktopProduct(产品资料管理)
      image
    • Main启动项目中 Shell 是有一个的简单布局 右侧导航 左侧显示主内容区。
      image

    • PrismDemo.DesktopCompany模块 实现公司管理的导航和内容视图。在这些项目里将来用MVVM模式。
      image
    • PrismDemo.DesktopProduct模块 同理

    上面的内容同一般建立一个简单Prism程序没有什么区别

    • Bootstrapper继承自 UnityBootstrapper
    class Bootstrapper : UnityBootstrapper
    {
    
       //…
    
    }
    • 重载 CreateModuleCatalog() 返回一个 ConfigurationModuleCatalog

    protected override IModuleCatalog CreateModuleCatalog()
    {
        return new ConfigurationModuleCatalog();
    }

    • 然后配置 App.config
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <configSections
      <section name="modules" type="Microsoft.Practices.Prism.Modularity.ModulesConfigurationSection, Microsoft.Practices.Prism"/> 
    </configSections
    <startup
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    </startup
    <modules
      <module assemblyFile="PrismDemo.DesktopCompany.dll" 
              moduleType="PrismDemo.DesktopCompany.CompanyModule, PrismDemo.DesktopCompany" 
              moduleName="CompanyModule" 
              startupLoaded="true" /> 
      <module assemblyFile="PrismDemo.DesktopProduct.dll" 
              moduleType="PrismDemo.DesktopProduct.ProductModule, PrismDemo.DesktopProduct" 
              moduleName="ProductModule" 
              startupLoaded="true"
        <!--<dependencies> 
          <dependency moduleName="ModuleE"/> 
        </dependencies>--> 
      </module
    </modules>
    • Unity Container 的配置没有什么特别导入配置即可

    protected override void ConfigureContainer()
          {
             UnityConfigurationSection config = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;
              config.Configure(this.Container);

              base.ConfigureContainer();
          }

    配置方式

    <configSections> 
      <section name="modules" type="Microsoft.Practices.Prism.Modularity.ModulesConfigurationSection, Microsoft.Practices.Prism"/> 
      <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/> 
    </configSections>
    
    <unity> 
      <typeAliases> 
        <!--三个不同类型的生命周期--> 
        <typeAlias alias="singleton" 
                   type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" /> 
        <typeAlias alias="external" 
                   type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity" /> 
        <typeAlias alias="perThread" 
                   type="Microsoft.Practices.Unity.PerThreadLifetimeManager, Microsoft.Practices.Unity" /> 
        
    
        <!--给类注册别名,以后直接使用别名就可以代替具体类,type=类的详细命名空间和类名词,然后逗号后面是程序集名词--> 
        <typeAlias alias="ProductListView" type="PrismDemo.DesktopProduct.Views.ProductListView, PrismDemo.DesktopProduct" /> 
        <typeAlias alias="ProductMenuView" type="PrismDemo.DesktopProduct.Views.ProductMenuView, PrismDemo.DesktopProduct" /> 
        <typeAlias alias="CompanyListView" type="PrismDemo.DesktopCompany.Views.CompanyListView, PrismDemo.DesktopCompany" /> 
        <typeAlias alias="CompanyMenuView" type="PrismDemo.DesktopCompany.Views.CompanyMenuView, PrismDemo.DesktopCompany" /> 
        <typeAlias alias="UIMessagesService" type="PrismDemo.UIMessage.Service.UIMessagesService, PrismDemo.UIMessage" /> 
        
      </typeAliases> 
      <containers> 
        <container> 
          <types> 
            <!--type是接口,mapto是目标实例化对象--> 
            <type type="ProductListView" mapTo="ProductListView" > 
              <lifetime type="singleton" /> 
            </type> 
            <type type="ProductMenuView" mapTo="ProductMenuView" > 
              <lifetime type="singleton" /> 
            </type> 
            <type type="CompanyListView" mapTo="CompanyListView" > 
              <lifetime type="singleton" /> 
            </type> 
            <type type="CompanyMenuView" mapTo="CompanyMenuView" > 
              <lifetime type="singleton" /> 
            </type> 
            <type type="UIMessagesService" mapTo="UIMessagesService" > 
              <lifetime type="singleton" /> 
            </type> 
          </types> 
        </container> 
      </containers> 
    </unity>
    
     

    一个简单的 就完成了

    image

    例子本身不完善,后面会继续改进。

    附件

  • 相关阅读:
    函数式编程(二):curry
    函数式编程(一):纯函数
    用 gulp 建一个服务器
    深度学习-Tensorflow2.2-预训练网络{7}-迁移学习基础针对小数据集-19
    深度学习-Tensorflow2.2-自定义训练综合实例与图片增强{6}-猫狗数据集实例-18
    深度学习-Tensorflow2.2-Tensorboard可视化{5}-可视化基础-17
    深度学习-Tensorflow2.2-Eager模式与自定义训练{4}-微分运算训练练习-16
    深度学习-Tensorflow2.2-卷积神经网络{3}-电影评论数据分类/猫狗数据集实例-15
    深度学习-Tensorflow2.2-批标准化简介-14
    深度学习-Tensorflow2.2-卷积神经网络{3}-卫星图像识别卷积综合实例(二分类)-13
  • 原文地址:https://www.cnblogs.com/bikaqiou2000/p/3101823.html
Copyright © 2011-2022 走看看