一步步实现 Prism + MEF(一)--- 搭建框架
第一步:构建一个名为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(一)--- 搭建框架的更多相关文章
- 一步步实现 Prism + MEF(二)--- 绑定命令
Prism程序集为我们提供了DelegateCommand命令,使用该命令可实现窗口直接绑定.第一步:在ViewModel中定义一个DelegateCommand属性. public Delegate ...
- Prism for WPF 搭建一个简单的模块化开发框架(一)
原文:Prism for WPF 搭建一个简单的模块化开发框架(一) 最近闲来无事又想搞搞WPF..... 做个框架吧,可能又是半途而废....总是坚持不下来 不废话了, 先看一下工程结构 布局大概是 ...
- Prism&MEF构建开发框架 (一)
Shell框架XECA shell.xaml主要起到是一个容器或壳的作用 <Window x:Class="XECA.Shell" xmlns="http ...
- 基于MEF的插件框架之总体设计
基于MEF的插件框架之总体设计 1.MEF框架简介 MEF的全称是Managed Extensibility Framework(MEF),其是.net4.0的组成部分,在3.5上也可以使用.熟悉ja ...
- Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单
原文:Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单 昨天晚上把TreeView的样式做了一下,今天给TreeView绑了数据,实现了切换页面功能 上 ...
- 初学springMVC搭建框架过程及碰到的问题
刚刚开始学spring框架,因为接了一个网站的项目,想用spring+springMVC+hibernate整合来实现它,现在写下搭建框架的过程及碰到的问题.希望给自己看到也能让大家看到不要踏坑. 一 ...
- 从零开始搭建框架SSM+Redis+Mysql(二)之MAVEN项目搭建
从零开始搭建框架SSM+Redis+Mysql(二)之MAVEN项目搭建 废话不说,直接撸步骤!!! 1.创建主项目:ncc-parent 选择maven创建项目,注意在创建项目中,packing选择 ...
- 从零开始搭建框架SSM+Redis+Mysql(一)之摘要
从零开始搭建框架SSM+Redis+Mysql(一)之摘要 本文章为本人实际的操作后的回忆笔记,如果有步骤错漏,希望来信307793969@qq.com或者评论指出. 本文章只体现过程,仅体现操作流程 ...
- 网络基础、ftp任务(进度条、计算文件大小、断点续传、搭建框架示例)
一.网络基础 1.端口,是什么?为什么要有端口? 端口是为了将同一个电脑上的不同程序进行隔离. IP是找电脑:端口是找电脑上的应用程序: 端口范围:1 – 65535 : 1 - 1024 不要 ...
随机推荐
- IOS GameCenter验证登陆
#import "GameKitHelper.h" #import "GameConstants.h" @interface GameKitHelper () ...
- HTML5+ Android打包证书
HBuilder默认App云端打包默认使用的是DCloud公用证书,其信息如下: MD5: 59:20:1C:F6:58:92:02:CB:2C:DA:B2:67:52:47:21:12 SHA1:B ...
- 【题解】At2370 Piling Up
[题解]At2370 Piling Up \[ dp(i,j,0/1) \\ 正在进行i项操作并且此时黑球剩下j个,黑球[0/1]数量曾经到过0 \\ 为什么加第二位,判重.怎么想到的? \] 非常神 ...
- 百度小程序转换微信小程序
Python脚本,一键转换Github地址:https://github.com/DWmelon/py-transfer-BdToWx 运行条件 具备Python环境,可在命令行中使用Python命令 ...
- @Transactional注解不回滚原因详解
最近试了试spring的回滚功能,根据网上的教程配置怎么都不好使,遂寻找答案, 网上的答案都是这么讲的: 1. 检查你方法是不是public的. 2. 你的异常类型是不是unchecked异常.如果我 ...
- Android Studio快速添加Gson Gsonformat
一.Android Studio快速添加Gson 具体操作: 1.File->Project Structure: 2.app->Dependencies->" ...
- Database: coursera assignment 1
q.1: Find the titles of all movies directed by Steven Spielberg. select title from moviewhere direct ...
- jQuery 3D旋转展示焦点图
在线演示 本地下载
- 在IIS中某一个网站启用net.tcp
绑定 高级设置 http和net.tcp用逗号分隔 //擦擦擦,见鬼了,下面的是tcp.net导致我找了好久,都找不出这个错误 //一定要注意,不要写错了. 否则会收到提示:找不到具有绑定 NetT ...
- 算法(Algorithms)第4版 练习 1.3.23 1.3.22
1.3.23 When it comes time to update t.next, x.next is no longer the original node following x, but i ...