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;
        }

四、运行界面如下

 

posted @ 2017-09-05 15:05  beautifulday  阅读(672)  评论(0编辑  收藏  举报