Prism中使用MEF(依赖注入)案例
来源:互联网 发布:java接收post请求数据 编辑:程序博客网 时间:2024/06/07 08:33
具体使用步骤如下:
第一步:构建一个名为Bootstrapper的类作为引导程序。
class Bootstrapper : MefBootstrapper { }
第二步:在MainWindow窗体中添加一个CoontentControl控件作为模块的容器,并在后台代码中添加[Export]属性以便MEF可以注入。
窗体代码:
引用命名空间 xmlns:prism="http://www.codeplex.com/prism"
<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="24" 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(); }
0 0
- Prism中使用MEF(依赖注入)案例
- Prism中使用MEF(依赖注入)案例 2
- silverlight 学习笔记 (八):Prism中MEF的初步认识
- 一步步实现 Prism + MEF(一)--- 搭建框架
- 一步步实现 Prism + MEF(二)--- 绑定命令
- 依赖注入简单案例
- Spring DI 依赖注入案例(带参数构造方法依赖注入、setter方法依赖注入、p名称空间注入)
- seasar2中使用ognl实现依赖注入
- Android中使用Dagger2进行依赖注入
- WebAPI 中使用Unity依赖注入
- MEF程序设计指南二:Silverlight中使用CompositionInitializer宿主MEF
- MEF程序设计指南二:Silverlight中使用CompositionInitializer宿主MEF
- MEF程序设计指南二:Silverlight中使用CompositionInitializer宿主MEF
- MVC实用架构设计:使用MEF应用IOC(依赖倒置)(1)
- Spring(三)入门案例DI(依赖注入)
- Prism与MVVM、Unity、MEF关系
- Prism与MVVM、Unity、MEF关系
- MEF学习系列(6): 在Silverlight中应用MEF
- 相关指针的理解
- windows7 x64 环境下的 opencv 3.2.0 在qt5.8.0(msvc 2015)上使用的配置
- 如何卸载rpm包
- Java基础———个简单的Java框架
- 如何删除在使用jQuery变量的选项标签?
- Prism中使用MEF(依赖注入)案例
- 改善脑回路1——斐波那契数列
- 关于java maven 项目debug运行时,项目报sourse not found问题
- __va_args__
- javascript 新语法
- 前端“Gui工程师”是现在和未来前端开发的职业方向
- Android中实现截图保存到指定目录或者系统图片,并支持批量删除
- Tensorflow ConfigProto & inter_/intra_op_parallelism_threads 整理
- 复写纸