【最佳实践】【Prism】使用mef的wpf/silverlight例子

一个基本的例子,没有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,复制到目录属性选择"始终复制"或"如果较新则复制"

   

到此完成

转载于:https://www.cnblogs.com/ningth/archive/2011/12/04/2275133.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值