第一步:构建一个名为Bootstrapper的类作为引导程序。

    class Bootstrapper : MefBootstrapper
{
}

第二步:在MainWindow窗体中添加一个CoontentControl控件作为模块的容器,并在后台代码中添加[Export]属性以便MEF可以注入。

窗体代码:

<ContentControl prism:RegionManager.RegionName="MainRegion" />

后台代码:

    using System.ComponentModel.Composition;

    [Export(typeof(MainWindow))]
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}

第三步:在Bootstrapper类中重写CreateShell方法以便返回一个Shell(MainWindow)实例。

        protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<MainWindow>();
}

第四步:在Bootstrapper类中重写InitializeShell方法以便启动Shell。

        protected override void InitializeShell()
{
Application.Current.MainWindow.Show();
}

第五步:在Bootstrapper类中重写ConfigureAggregateCatalog方法,将所有带有[Export]属性的类所在的程序集目录添加进去。

        protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleAModule).Assembly));
}

第六步:创建一个类库项目MoudleA,添加类ModuleAModule并实现IModule接口,为该类添加[ModuleExport]属性以便让该类成为Prism模块初始化类。

    [ModuleExport(typeof(ModuleAModule))]
public class ModuleAModule : IModule
{
IRegionManager _regionManager; // 当Prism加载该模块时,它将通过MEF实例化该类,MEF将注入一个Region Manager实例
[ImportingConstructor]
public ModuleAModule(IRegionManager regionManager)
{
_regionManager = regionManager;
} // 该方法将为模块启动提供一个代码入口点
// 我们将把MEF容器里的ViewA注入到MainWindow界面定义的MainRegion中
public void Initialize()
{
_regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewA));
}
}

第七步:在ModuleA项目中添加ViewA用户控件,并在后台代码中添加[Export]属性,以便MEF在需要的时候能注入它。

窗口代码:

    <Grid>
<!-- 绑定ViewModel中的Title属性 -->
<TextBlock Text="{Binding Title}" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="" FontWeight="Bold"></TextBlock>
</Grid>

后台代码:

    [Export(typeof(ViewA))]
public partial class ViewA : UserControl
{
public ViewA()
{
InitializeComponent();
}
}

第八步:在ModuleA项目中添加ViewAViewModel类

    [Export(typeof(ViewAViewModel))]
public class ViewAViewModel : BindableBase
{
private string _title = "Hello World";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
}

完成上述步骤之后就可以在App.xaml.cs中直接运行Bootstrapper引导程序。

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

一步步实现 Prism + MEF(一)--- 搭建框架的更多相关文章

  1. 一步步实现 Prism + MEF(二)--- 绑定命令

    Prism程序集为我们提供了DelegateCommand命令,使用该命令可实现窗口直接绑定.第一步:在ViewModel中定义一个DelegateCommand属性. public Delegate ...

  2. Prism for WPF 搭建一个简单的模块化开发框架(一)

    原文:Prism for WPF 搭建一个简单的模块化开发框架(一) 最近闲来无事又想搞搞WPF..... 做个框架吧,可能又是半途而废....总是坚持不下来 不废话了, 先看一下工程结构 布局大概是 ...

  3. Prism&MEF构建开发框架 (一)

    Shell框架XECA shell.xaml主要起到是一个容器或壳的作用 <Window x:Class="XECA.Shell"      xmlns="http ...

  4. 基于MEF的插件框架之总体设计

    基于MEF的插件框架之总体设计 1.MEF框架简介 MEF的全称是Managed Extensibility Framework(MEF),其是.net4.0的组成部分,在3.5上也可以使用.熟悉ja ...

  5. Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单

    原文:Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单 昨天晚上把TreeView的样式做了一下,今天给TreeView绑了数据,实现了切换页面功能 上 ...

  6. 初学springMVC搭建框架过程及碰到的问题

    刚刚开始学spring框架,因为接了一个网站的项目,想用spring+springMVC+hibernate整合来实现它,现在写下搭建框架的过程及碰到的问题.希望给自己看到也能让大家看到不要踏坑. 一 ...

  7. 从零开始搭建框架SSM+Redis+Mysql(二)之MAVEN项目搭建

    从零开始搭建框架SSM+Redis+Mysql(二)之MAVEN项目搭建 废话不说,直接撸步骤!!! 1.创建主项目:ncc-parent 选择maven创建项目,注意在创建项目中,packing选择 ...

  8. 从零开始搭建框架SSM+Redis+Mysql(一)之摘要

    从零开始搭建框架SSM+Redis+Mysql(一)之摘要 本文章为本人实际的操作后的回忆笔记,如果有步骤错漏,希望来信307793969@qq.com或者评论指出. 本文章只体现过程,仅体现操作流程 ...

  9. 网络基础、ftp任务(进度条、计算文件大小、断点续传、搭建框架示例)

    一.网络基础 1.端口,是什么?为什么要有端口? 端口是为了将同一个电脑上的不同程序进行隔离. IP是找电脑:端口是找电脑上的应用程序: 端口范围:1 – 65535 :    1 - 1024 不要 ...

随机推荐

  1. visual studio 2010 c++ 打印 Hello world

    由于好奇心驱使温习下c高级简化语言语言(个人解释可能不太准确).下面用visual studio 2010 实现 HelloWord 打印 第一步:visual studio 2010 打开.文件-- ...

  2. m*n matrix min rank square matrix

    m*n matrix m*n=1000 f(A)=25 https://www.cs.princeton.edu/courses/archive/spring12/cos598C/svdchapter ...

  3. mysqldump的--master-data参数

    mysqldump导出数据时,当这个参数的值为1的时候,mysqldump出来的文件就会包括CHANGE MASTER TO这个语句,CHANGE MASTER TO后面紧接着就是file和posit ...

  4. ABAP 程序运行时间记录表

    自建表记录程序运行时间,测试程序效率,可作为系统优化工具.

  5. (转) 在linux网络UDP通信中,关于客户端是否绑定的理解

    最近在做一个实例,是用RTSP协议完成.服务器已经有了,只需要把客户端做好就行了,在做的过程中发现了一些问题,就是关于UDP客户端是否绑定的问题. 也许大家在书上看到的大多都是说UDP客户端不需要绑定 ...

  6. Java for LeetCode 088 Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 解题思路一: ...

  7. Java for LeetCode 096 Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  8. Ruby 文件 FILE

    FileUtils.makedirs(LOCAL_DIR) unless File.exists?LOCAL_DIR require 'fileutils' Dir.mkdir(DATA_DIR) u ...

  9. Algorithm: bit manipulation

    1. 一个数的从右起第p1位和第p2位swap n位 unsigned int swapBits(unsigned int x, unsigned int p1, unsigned int p2, u ...

  10. hihocoder hiho第38周: 二分·二分答案 (二分搜索算法应用:二分搜索值+bfs判断可行性 )

    题目1 : 二分·二分答案 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回和上上回里我们知道Nettle在玩<艦これ>,Nettle在整理好舰队之后 ...