Prism中使用MEF的例子
一个基本的例子,没有viewmodel,没有使用Behaviors
大体步骤:
1、创建应用程序
2、使用"Shell"替换"MainWindow"(silverlight替换MainPage)
3、创建Bootstrapper(引导程序)
4、创建模块
5、加入视图
1、创建程序
使用vs 2010创建wpf或silverlight应用程序,添加以下引用
Microsoft.Practices.Prism
Microsoft.Practices.Prism.MefExtensions
System.ComponentModel.Composition
2、修改MainWindow.cs或MainPage.cs为Shell.cs
在代码视图中,右键点MainWindow或MainPage选择重构--〉重命名,命名为Shell
修改App.xaml
wpf程序去掉starturi属性
修改App.xaml.cs
Startup事件中
private void Application_Startup(object sender, StartupEventArgs e)
{
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
导出Shell
[Export]
public partial class Shell : Window
{
public Shell()
{
InitializeComponent();
}
}
3、创建Bootstrapper
添加Bootstrapper类,注意wpf/silverlight在InitializeShell中的区别
using Microsoft.Practices.Prism.MefExtensions;
using System.ComponentModel.Composition.Hosting;
using System.IO;
namespace WpfApplication
{
class Bootstrapper : MefBootstrapper
{
protected override System.Windows.DependencyObject CreateShell()
{
return this.Container.GetExportedValue<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
#if SILVERLIGHT
App.Current.RootVisual = (Shell)this.Shell;
#else
App.Current.MainWindow = (Shell)this.Shell;
App.Current.MainWindow.Show();
#endif
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
//加载自身
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(this.GetType().Assembly));
//加载目录
if (Directory.Exists("./Modules"))
{
this.AggregateCatalog.Catalogs.Add(new DirectoryCatalog("./Modules"));
}
}
}
}
4、创建模块
加入以下引用
Microsoft.Practices.Prism
Microsoft.Practices.Prism.MefExtensions
System.ComponentModel.Composition
加入模块初始化用的类如:MarketModule,实现Initialize
[ModuleExport(typeof(MarketModule))]
public class MarketModule: IModule
{
[Import]
public IRegionManager TheRegionManager { private get; set; }
public void Initialize()
{
TheRegionManager.RegisterViewWithRegion("MarketRegion", typeof(MarketView));
}
}
5、加入视图,并导出
[Export(typeof(MarketView))]
public partial class MarketView : UserControl
{
public MarketView()
{
InitializeComponent();
}
}
6、修改Shell.xaml
加入prism命名空间
xmlns:prism="http://www.codeplex.com/prism"
Grid中加入
<ItemsControl prism:RegionManager.RegionName="MarketRegion"/>
7、应用程序中创建文件夹Modules
在Modules文件中,加入现有项(以链接方式)"模块名称.dll" 如Modules.Market.dll
模块名称.dll,复制到目录属性选择"始终复制"或"如果较新则复制"
转载:http://www.cnblogs.com/ningth/archive/2011/12/04/2275133.html
Prism中使用MEF的例子的更多相关文章
- .NET MEF入门级例子
学习新东西,喜欢从简单的例子入手,感觉理解和上手会快点,本文记录下我做的一个简单的mef的例子,至于理论的话百度,谷歌多的去了. Mef可以在你调整了某些功能的时候不需要重新去做代码,只需要换掉相应的 ...
- 在.NET Core中使用MEF
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:微软的可托管扩展框架也移植到.NET Core上了. 可托管扩展框架(Managed ...
- Spark(Python) 从内存中建立 RDD 的例子
Spark(Python) 从内存中建立 RDD 的例子: myData = ["Alice","Carlos","Frank"," ...
- 【转】MEF程序设计指南一:在应用程序中宿主MEF
在应用程序中宿主MEF其实非常简单,只需要创建一个组合容器对象(CompositionContainer)的实例,然后将需要组合的部件(Parts)和当前宿主程序添加到容器中即可.首先需要添加MEF框 ...
- 在WinRT程序中使用MEF
今天试了一下在WinRT中使用MEF,这里简单的介绍一下步骤. 首先,使用NuGet安装MEF 然后,就可以使用MEF组装插件了,简单的示例如下: interface ILogger { ...
- MEF 编程指南(一):在应用中托管 MEF
在应用程序中托管(Hosing) MEF 涉及到创建组合容器(CompositionContainer) 实例,添加可组合部件(Composable Parts),包括应用程序宿主(Host)本身并进 ...
- php中的一些编程例子
#一到一百不能被三整除的数 for($i=1;$i<=100;$i++){ if($i%3 != 0){ $arr[] = $i; }} var_dump($arr); #水仙花数for($i= ...
- SQL Server 中同时操作的例子:
在SQL 中同一逻辑阶段的操作是同时发生的. 先有一个例子做为带入: declare @x as int =1;declare @y as int =2;set @x=@y;set @y=@x;sel ...
- [shiro学习笔记]第三节 使用myeclipse导入apache shiro中的QuikStart example例子
本文地址:http://blog.csdn.net/sushengmiyan/article/details/40149131 shiro官网:http://shiro.apache.org/ shi ...
随机推荐
- 适当使用enum做数据字典 ( .net c# winform csharp asp.net webform )
在一些应用中,通常会用到很多由一些常量来进行描述的状态数据,比如性别(男.女),审核(未审核.已审核)等.在数据库中一般用数字形式来存储,比如0.1等. 不好的做法 经常看到一些应用(ps:最近又看到 ...
- Atitit.论图片类型 垃圾文件的识别与清理 流程与设计原则 与api概要设计 v2 pbj
Atitit.论图片类型 垃圾文件的识别与清理 流程与设计原则 与api概要设计 v2 pbj 1. 俩个问题::识别垃圾文件与清理策略1 2. 如何识别垃圾图片1 2.1. 体积过小文件<1 ...
- 集合1--毕向东java基础教程视频学习笔记
Day14 集合框架01 体系概述02 共性方法03 迭代器04 List集合共性方法05 ListIterator06 List集合具体对象特点07 Vector中的枚举 01 体系概述 集合类为什 ...
- JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns
In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...
- 预写式日志(Write-Ahead Logging (WAL))
SQL Server中使用了WAL(Write-Ahead Logging)技术来保证事务日志的ACID特性.而且大大减少了IO操作. WAL的核心思想是:在数据写入到数据库之前,先写入到日志.再将日 ...
- mysql 5.5多实例部署【图解】
mysql5.5数据库多实例部署,我们可以分以下几个步骤来完成. 1. mysql多实例的原理 2. mysql多实例的特点 3. mysql多实例应用场景 4. mysql5.5多实例部署方法 一. ...
- python-1 python基础知识
python第一课代码笔记 hello world [root@heartbeat-data- python]# vim hello1.py #!/usr/bin/env python print ( ...
- JavaScript 产生指定范围随机数
function selectFrom(lowerValue, upperValue) { var choices = upperValue - lowerValue + 1; return Math ...
- 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式
在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式 转载自:http://blog.163.com/smhily_min/blog/static/75206226201092011 ...
- 树莓派mjpg-stream摄像头监控
Q:463431476 第一步: 1.安装依赖 sudo apt-get install libv4l-dev libjpeg8-dev imagemagick 第二步: 下载SVN sudo ...