一个基本的例子,没有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. Windows 编程中的字符串(2)

    (1)windows写日志系统 void writeDebugEventLog(TCHAR* pszMessage, WORD wType) { //#ifdef _DEBUG HANDLE hEve ...

  2. HTML <fieldset> 标签

    <div style="height:360px;width:180"> <fieldset> <legend> 用户管理 </legen ...

  3. 面试题整理:SQL(一)

    1.横纵表转换 A表 Name Course Grade Alex English 80 Alex Chinese 70 Alex Japanese 85 Bob English 75 Bob Chi ...

  4. Java Gradle入门指南之依赖管理(添加依赖、仓库、版本冲突)

        开发任何软件,如何管理依赖是一道绕不过去的坎,软件开发过程中,我们往往会使用这样那样的第三方库,这个时候,一个好的依赖管理就显得尤为重要了.作为一个自动构建工作,Gradle对依赖管理有着很好 ...

  5. C#编写抽奖问题

    输入每个人的中奖号码,进行滚动显示    //清屏  //随即一个索引   // 根据索引打印  //等待0.1秒            Console.Write("请输入参与者人数:&q ...

  6. [Linux 性能检测工具]SAR

    SAR NAME: SAR报告,收集,保存系统活动信息 语法: sar  [ -A ] [ -b ] [ -B ] [ -C ] [ -d ] [ -h ] [ -i interval ] [ -m ...

  7. 今天说一下 Group by 这个东西

    group by 这个关键字,这个语句太平凡了~基本上只要有报表的地方,就会有它的身影. 常规用法就是 INSERT INTO #TypeValue ( TypeID, Col2 ) , N , N ...

  8. Runtime.exec() sucks!!!!

    自己项目中使用到了 Runtime rt = Runtime.getRuntime(); Process p = rt.exec("query session");p.waitFo ...

  9. MySQL 5.6 主从复制如何处理——触发器,函数,存储过程,调度事件

      截图来自MySQL5.6的pdf版文档. 说明: 1)基于语句的复制时,trigger会在slave上执行,所以slave上也需要有trigger的定义,不然会导致主从数据不一致的: 2)基于行的 ...

  10. 010 使用netmap API接管网卡,接收数据包,回应ARP请求

    一.本文目的: 上一节中,我们已经在CentOS 6.7 上安装好了netmap,也能接收和发送包了,这节我们来调用netmap中的API,接管网卡,对网卡上收到的数据包做分析,并回应ARP请求. 二 ...