先简单的介绍一下 Prism 框架,引用微软官方的解释:
Prism provides guidance to help you more easily design and build, flexible, and easy-to-maintain client business apps that run on Windows Runtime, Windows Presentation Foundation (WPF) desktop, Silverlight, or Windows Phone 7. These apps may start small and evolve over time.
官方链接 https://msdn.microsoft.com/en-us/library/ff648465.aspx,可下载到文档和示例代码。
多的介绍就不必了,研究这套框架的人基本是做 WPF 或者 Silverlight 的人,我是新人,理解不深还请大神指教。听说 Prism 是开源的,做了才一年的小菜目前框架都用的不熟,以后再看源码吧。
Prism 要用到 IOC 容器,提供选择的有 Unity 和 MEF,这个系列的博文以后我都选用 MEF。不懂 MEF 的建议看看这位大牛的系列博文 http://www.cnblogs.com/yunfeifei/p/3922668.html
先说一下思路:
1、Prism 思想是模块化编程,我将主界面拆分为四个模块(A、B、C、D)。
2、模块之间不能互相引用,也就是解耦了。
3、目前就以上两点,要考虑到以后对项目进行扩展,所以预留一个 Infrastructure(Project)放置模块之间抽象的东西。
完成之后的界面图如下:
解决方案总体结构:
一、基本结构搭建
1、按照上图结构添加项目,注意只有 Desktop.MainWindow 中保留 App.xaml,并且输出类型是 Windows 应用程序,其余的 Project 都要删除 App.xaml,输出类型为类库。
2、删除 Desktop.MainWindow 中的默认的 MainWindow,新建一个 Shell 窗体,根据 Prism 的思想,这个 Shell 就是主窗体。
3、为所有的 Project 添加 Prism 的引用,我这里为所有的 Project 安装了以下四个 Prism 的 Nuget 包。
4、在 ModuleA 中新建类 ModuleA,ModuleB,ModuleC,ModuleD,继承自 IModule 接口,只有继承这个接口才能被加载进 Prism 中。
- using Prism.Modularity;
- using Prism.Mef.Modularity;
- using Prism.Regions;
- using System.ComponentModel.Composition;
- namespace ModuleA
- {
- [ModuleExport("ModuleA", typeof(ModuleA), InitializationMode = InitializationMode.WhenAvailable)]
- public class ModuleA : IModule
- {
- private readonly IRegionViewRegistry regionViewRegistry;
- [ImportingConstructor]
- public ModuleA(IRegionViewRegistry registry)
- {
- this.regionViewRegistry = registry;
- }
- public void Initialize()
- {
- regionViewRegistry.RegisterViewWithRegion("RegionA",typeof(View.GridA));
- }
- }
- }
注意最后一行代码:
- regionViewRegistry.RegisterViewWithRegion("RegionA", typeof(View.GridA));
- RegionA就是主界面吧Shell中为ModuleA模块预留的位置,可以理解为占位符,View.GridA就是我们为ModuleA写的的界面。BCD模块同理。
5、添加一个 BootStrapper 类,这个类要继承自 MefBootstrapper。
Prism 提供四种配置模块加载的方式,看官方文档(这里我提供第一种和第二种方式):
- using Prism.Mef;
- using Prism.Modularity;
- using Prism;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.Composition.Hosting;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using ModuleA;
- namespace Desktop.MainWindow
- {
- public class BootStrapper:MefBootstrapper
- {
- protected override DependencyObject CreateShell()
- {
- return this.Container.GetExportedValue<Shell>();
- }
- protected override void InitializeShell()
- {
- base.InitializeShell();
- Application.Current.MainWindow = (Shell)this.Shell;
- Application.Current.MainWindow.Show();
- }
- protected override void ConfigureAggregateCatalog()
- {
- base.ConfigureAggregateCatalog();
- this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));
- //this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(this.GetType().Assembly));
- //第一种加载方式
- this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA.ModuleA).Assembly));
- this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleB.ModuleB).Assembly));
- this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleC.ModuleC).Assembly));
- this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleD.ModuleD).Assembly));
- }
- protected override IModuleCatalog CreateModuleCatalog()
- {
- // When using MEF, the existing Prism ModuleCatalog is still the place to configure modules via configuration files.
- return new ConfigurationModuleCatalog();
- }
- //Prism提供四种加载模块的方式,以下是第二种从xaml文件中加载,注意文件要生成Resource,并且始终复制到输出目录
- //protected override IModuleCatalog CreateModuleCatalog()
- //{
- // this.ModuleCatalog = new ModuleCatalog();
- // var xamlCatalog = Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("ModuleCatalogs.xaml", UriKind.Relative));
- // //var xamlCatalog = Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("ModuleCatalogs.xaml", UriKind.Relative));
- // foreach (var item in xamlCatalog.Modules)
- // {
- // ModuleCatalog.AddModule(item);
- // }
- // return xamlCatalog;
- //}
- }
- }
6、在 App.xaml 中把启动项设置为 BootStrapper 应该都会吧,不会的看源码哦。至此,一个简单的基于 MEF 的 Prism 框架就搭建好了。链接提供源码。
来源: http://www.cnblogs.com/lelehellow/p/8036655.html