MefBootstrapper在Prism引导程序中的使用

前言

MefBootstrapper 是使用 MEF(Managed Extensibility Framework)作为依赖注入容器的 Prism 引导程序。

1、创建Bootstrapper引导程序类

CreateShell方法用于从容器中获取主窗口;InitializeShell中用于设置主窗体以及显示主窗体,注意这里要引用Prism.Mef.Wpf.dll。

csharp 复制代码
 class BootStrapper : MefBootstrapper
    {
        private readonly string ApplicationPath = Environment.CurrentDirectory + "\\lib\\";
        protected override DependencyObject CreateShell()
        {
            return this.Container.GetExportedValue<MyShell>();
        }
        protected override void InitializeShell()
        {
            base.InitializeShell();
            Application.Current.MainWindow = (Window)Shell;
            Application.Current.MainWindow.Show();
        }

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));
            DirectoryCatalog catalog = new DirectoryCatalog(ApplicationPath);
            this.AggregateCatalog.Catalogs.Add(catalog);
        }
        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();
        }
    }
csharp 复制代码
启动流程
Application.Startup()
    ↓
BootStrapper 实例化
    ↓
CreateLogger() - 创建日志
    ↓
ConfigureModuleCatalog- 配置模块目录
    ↓
ConfigureAggregateCatalog- 配置组件目录
    ↓
CreateShell() - 创建主窗口
    ↓
InitializeShell() - 初始化主窗口
    ↓
InitializeModules() - 初始化所有模块
    ↓
应用程序就绪

2、使用引导程序

1)删除 StartupUri

2)重写OnStartup方法

在App.xaml.cs类中重写OnStartup方法,实例化BootStrapper类,并调用Run方法

3、导出主窗体MyShell类

csharp 复制代码
  public partial class App : Application
    {

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            BootStrapper bootStrapper = new BootStrapper();
            bootStrapper.Run();
        }
    }

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》

2、《C#与PLC通信从入门到精通 》

3、《C# Modbus通信从入门到精通》

4、《C#Socket通信从入门到精通 》

5、《C# MES通信从入门到精通》

6、《winform控件从入门到精通》

7、《C#操作MySql数据库从入门到精通》

相关推荐
EIP低代码平台4 小时前
EIP低代码平台-系统参数配置详解|落地三级等保+高灵活私有化部署方案
低代码·c#·权限·工作流·netcore
王莎莎-MinerU5 小时前
MCP 解决的是工具接入,科研 Agent 还缺的是科学证据接口标准化
开发语言·网络·人工智能·深度学习·pdf·c#·php
-银雾鸢尾-6 小时前
C#中Object类内的方法
开发语言·c#
weixin_727535627 小时前
双Token认证体系深度拆解:Spring Security + JWT + Redis
redis·spring·wpf
tiankong12137 小时前
如何拓展多态下的子类
设计模式·c#
geats人山人海8 小时前
c# 第九章 record
开发语言·c#
-银雾鸢尾-9 小时前
C#中的抽象类与抽象方法
开发语言·c#
liuxiaowei310 小时前
Winform+WPF双框架实战:喷涂工艺SCADA上位机从0到1搭建(附采集监控源码+车间踩坑实录)
大数据·hadoop·wpf
影寂ldy10 小时前
C# Task 进阶:WaitAll / WaitAny / WhenAll / WhenAny
开发语言·c#