zoukankan      html  css  js  c++  java
  • wpf prism加载独立模块实例

    一、首先看看文件的组织架构

    module1 module2生成dll某块。Shell来显示管理模块

    二,看看关键bootstrapper类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace wpfPrismTest
    {
        using System.ComponentModel.Composition;
        using Microsoft.Practices.Prism.MefExtensions;
        using System.Windows;
        using Microsoft.Practices.Prism.Modularity;
        using System.ComponentModel.Composition.Hosting;
        using System.IO;  
        class QuickStartBootstrapper:MefBootstrapper
        {
            protected override DependencyObject CreateShell()
            {
                return this.Container.GetExportedValue<Shell>();
            }

            protected override void InitializeShell()
            {
                base.InitializeShell();

                Application.Current.MainWindow = (Shell)this.Shell;
                Application.Current.MainWindow.Show();
            }
            protected override void ConfigureContainer()
            {
                base.ConfigureContainer();
                AggregateCatalog.Catalogs.Add(new AssemblyCatalog(this.GetType().Assembly));

                if (Directory.Exists("./Modules"))
                {
                    this.AggregateCatalog.Catalogs.Add(new DirectoryCatalog("./Modules"));
                }
            }
            //protected override void ConfigureAggregateCatalog()
            //{
            //    base.ConfigureAggregateCatalog();

            //    // Add this assembly to export ModuleTracker
            //    this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(QuickStartBootstrapper).Assembly));


            //    // Module B and Module D are copied to a directory as part of a post-build step.
            //    // These modules are not referenced in the project and are discovered by inspecting a directory.
            //    // Both projects have a post-build step to copy themselves into that directory.
            //    DirectoryCatalog catalog = new DirectoryCatalog("Modules");
            //    this.AggregateCatalog.Catalogs.Add(catalog);
            //}
            protected override IModuleCatalog CreateModuleCatalog()
            {
                // When using MEF, the existing Prism ModuleCatalog is still the place to configure modules via configuration files.

                return new ConfigurationModuleCatalog();
                //return new DirectoryModuleCatalog() { ModulePath = @".Modules" };

            }
        }
    }
    还有module1 module类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Practices.Prism;

    using Microsoft.Practices.Prism.MefExtensions;
    using Microsoft.Practices.Prism.Modularity;
    using System.ComponentModel.Composition;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.Prism.MefExtensions.Modularity;

    namespace Module1
    {
        [ModuleExport(typeof(Module1), InitializationMode = InitializationMode.OnDemand)]
        public class Module1:IModule
        {
            [Import]
            public IRegionManager TheRegionManager { private get; set; }
            /// <summary>
            /// Notifies the module that it has be initialized.
            /// </summary>
            public void Initialize()
            {
                TheRegionManager.RegisterViewWithRegion("MarketRegion1", typeof(ModuleView1));
            }
        }
    }
    三、在shell后端代码添加如下代码

            private void NavBarItem_Click(object sender, EventArgs e)
            {

                moduleManager.LoadModule("Module1");
                ItemsControl ic = new ItemsControl();
                RegionManager.SetRegionName(ic, "MarketRegion1");
                documentPanel.Content = ic;

            }

            private void NavBarItem_Click_1(object sender, EventArgs e)
            {
                ItemsControl ic = new ItemsControl();
                RegionManager.SetRegionName(ic, "MarketRegion2");
                documentPanel.Content = ic;
            }

    四、运行界面如下

  • 相关阅读:
    0005 数组(array)的静态和动态声明、最大值最小值、数组的反转、复制
    0004day04_15循环结构-循环嵌套、break和continue与标签随机数的另外一种方式 break、continue、求最大公约数、循环嵌套、求水仙花数 out标签等
    0003java.util.Scanner、输出语句、分支结构 if else 随机数 switch case选择结构和equals
    Maven配置阿里镜像
    中国大学MOOC-陈越、何钦铭-数据结构-2020春——最大子列和问题Java实现代码
    IDEA中jsp下out.println标红处理方法
    log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
    IDEA下Java项目中创建xml文件
    Eclipse中web项目的导出和导入操作步骤
    EL(Expression Language)表达式语言理解
  • 原文地址:https://www.cnblogs.com/tianmochou/p/7478683.html
Copyright © 2011-2022 走看看