先簡單的介紹一下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框架就搭建好了。鏈接提供源碼。