一个基本的例子,没有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的例子的更多相关文章

  1. .NET MEF入门级例子

    学习新东西,喜欢从简单的例子入手,感觉理解和上手会快点,本文记录下我做的一个简单的mef的例子,至于理论的话百度,谷歌多的去了. Mef可以在你调整了某些功能的时候不需要重新去做代码,只需要换掉相应的 ...

  2. 在.NET Core中使用MEF

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:微软的可托管扩展框架也移植到.NET Core上了. 可托管扩展框架(Managed ...

  3. Spark(Python) 从内存中建立 RDD 的例子

    Spark(Python) 从内存中建立 RDD 的例子: myData = ["Alice","Carlos","Frank"," ...

  4. 【转】MEF程序设计指南一:在应用程序中宿主MEF

    在应用程序中宿主MEF其实非常简单,只需要创建一个组合容器对象(CompositionContainer)的实例,然后将需要组合的部件(Parts)和当前宿主程序添加到容器中即可.首先需要添加MEF框 ...

  5. 在WinRT程序中使用MEF

    今天试了一下在WinRT中使用MEF,这里简单的介绍一下步骤. 首先,使用NuGet安装MEF 然后,就可以使用MEF组装插件了,简单的示例如下: interface ILogger    {     ...

  6. MEF 编程指南(一):在应用中托管 MEF

    在应用程序中托管(Hosing) MEF 涉及到创建组合容器(CompositionContainer) 实例,添加可组合部件(Composable Parts),包括应用程序宿主(Host)本身并进 ...

  7. php中的一些编程例子

    #一到一百不能被三整除的数 for($i=1;$i<=100;$i++){ if($i%3 != 0){ $arr[] = $i; }} var_dump($arr); #水仙花数for($i= ...

  8. SQL Server 中同时操作的例子:

    在SQL 中同一逻辑阶段的操作是同时发生的. 先有一个例子做为带入: declare @x as int =1;declare @y as int =2;set @x=@y;set @y=@x;sel ...

  9. [shiro学习笔记]第三节 使用myeclipse导入apache shiro中的QuikStart example例子

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/40149131 shiro官网:http://shiro.apache.org/ shi ...

随机推荐

  1. OC self = [super init] , 点语法 , @property

    OC self = [super init] , 点语法 , @property 构造方法为啥这么写? self = [super init]; [super init] 的结果可能有三种: 第一种: ...

  2. org.apache packages can't be resolved in Eclipse解决方案

    删除.m2仓库里面对应的jar包,重新Maven->update project

  3. C++语言-05-三大特性

    概述 C++ 是面向对象的语言,具备 OOP 的基本特性. 封装 概念 将数据和操作数据的函数绑定在一起 作用 避免受到外界的干扰和误用,确保了安全 与封装相关的概念 数据抽象 仅向用户暴露接口而把具 ...

  4. C#复习⑤

    C#复习⑤ 2016年6月19日 22:39 Main Inheritance 继承 1.继承的语法结构 class A { // base class int a; public A() {...} ...

  5. Git详解之三 Git分支

    相关文档 — 更多 Git 基础培训.ppt GIT 使用经验.ppt GIT 介绍.pptx GIT 分支管理是一门艺术.docx Eclipse上GIT插件EGIT使用手册.docx git/gi ...

  6. Tomcat 的使用学习

    一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...

  7. .Net Attribute详解(上)-Attribute本质以及一个简单示例

    Attribute的直接翻译是属性,这和Property容易产生混淆,所以一般翻译成特性加以区分.Attribute常常的表现形式就是[AttributeName], 随意地添加在class, met ...

  8. coursera机器学习-支持向量机SVM

    #对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...

  9. NuGet及快速安装使用

    关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...

  10. linux开机自动连接无线网络

           1.右击无线网络图标的“编辑连接”. 2.在“无线”选项卡里,选择“编辑”. 3.在“无线安全性”选项卡里,输入无线密匙,并选中左下角的“对所有用户可      用”的选项点击应用,会提 ...