一步步实现 Prism + MEF(一)--- 搭建框架
2017-12-18 10:10
471 查看
第一步:构建一个名为Bootstrapper的类作为引导程序。
第二步:在MainWindow窗体中添加一个CoontentControl控件作为模块的容器,并在后台代码中添加[Export]属性以便MEF可以注入。
窗体代码:
后台代码:
第三步:在Bootstrapper类中重写CreateShell方法以便返回一个Shell(MainWindow)实例。
第四步:在Bootstrapper类中重写InitializeShell方法以便启动Shell。
第五步:在Bootstrapper类中重写ConfigureAggregateCatalog方法,将所有带有[Export]属性的类所在的程序集目录添加进去。
第六步:创建一个类库项目MoudleA,添加类ModuleAModule并实现IModule接口,为该类添加[ModuleExport]属性以便让该类成为Prism模块初始化类。
第七步:在ModuleA项目中添加ViewA用户控件,并在后台代码中添加[Export]属性,以便MEF在需要的时候能注入它。
窗口代码:
后台代码:
第八步:在ModuleA项目中添加ViewAViewModel类
完成上述步骤之后就可以在App.xaml.cs中直接运行Bootstrapper引导程序。
class Bootstrapper : MefBootstrapper { }
第二步:在MainWindow窗体中添加一个CoontentControl控件作为模块的容器,并在后台代码中添加[Export]属性以便MEF可以注入。
窗体代码:
<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(); }
相关文章推荐
- 一步步实现 Prism + MEF(一)--- 搭建框架
- 一步步实现 Prism + MEF(二)--- 绑定命令
- 一步步实现 Prism + MEF(二)--- 绑定命令
- 一步步实现自己的框架系列(四):页面与页面服务的创建
- Android 教你一步步搭建MVP+Retrofit+RxJava网络请求框架
- 【Apache Mina2.0开发之一】搭建Apache Mina框架并实现Server与Client端消息传递 推荐
- Android 教你一步步搭建MVP+Retrofit+RxJava网络请求框架
- 完整使用SSM框架(Spring SpringMVC Mybatis)搭建web服务器实现登录功能
- Spring学习之SpringMVC框架快速搭建实现用户登录功能
- RadioGroup + Fragment实现项目框架搭建
- 一步步学习Hibernate框架(三):采用jpa实现一对多关联映射(一)
- 从零开始写javaweb框架笔记15-搭建轻量级JAVAWEB框架-实现依赖注入功能
- SSM框架day01——029——适配器模式测试环境搭建、030——多功能适配器实现、031——单功能适配器实现
- 搭建Apache Mina框架并实现Server与Client端的简单消息传递
- 从零开始写javaweb框架笔记14-搭建轻量级JAVAWEB框架-实现Bean容器
- 一步步实现Spring框架(一)项目搭建
- 实现网易新闻框架的搭建
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现
- [c#]asp.net开发微信公众平台(2)多层架构框架搭建和入口实现