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数据库从入门到精通》

相关推荐
BruceNeter1 天前
C# winform卡死问题分析1——线程池子线程创建句柄
c#·.net
samble2 天前
从零搭建半导体设备上位机(十二):参数设置页,表单校验、范围校验与配方联动
wpf·数据校验·validationrule
何以解忧唯有撸码3 天前
开源】当桌面图标开始打台球:我用 WinForms 做了一桌“真的”台球
c#·winform·自定义控件·gdi
国家一级摸鱼选手3 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
汤姆宝库4 天前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
Risehuxyc4 天前
C# 发展历史
开发语言·c#
kyle~4 天前
C# --- 反射
开发语言·c#
暴走约伯5 天前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
用户788477316346 天前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船6 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua