先簡單的介紹一下Prism框架,引用微軟官方的解釋:html
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,可下載到文檔和示例代碼。app
多的介紹就沒必要了,研究這套框架的人基本是作WPF或者Silverlight的人,我是新人,理解不深還請大神指教。據說Prism是開源的,作了才一年的小菜目前框架都用的不熟,之後再看源碼吧。框架
Prism要用到IOC容器,提供選擇的有Unity和MEF,這個系列的博文之後我都選用MEF。不懂MEF的建議看看這位大牛的系列博文http://www.cnblogs.com/yunfeifei/p/3922668.htmlide
先說一下思路:模塊化
一、Prism思想是模塊化編程,我將主界面拆分爲四個模塊(A、B、C、D)。post
二、模塊之間不能互相引用,也就是解耦了。flex
三、目前就以上兩點,要考慮到之後對項目進行擴展,因此預留一個Infrastructure(Project)放置模塊之間抽象的東西。ui
完成以後的界面圖以下:this
解決方案整體結構:
1、基本結構搭建
一、按照上圖結構添加項目,注意只有Desktop.MainWindow中保留App.xaml,而且輸出類型是Windows應用程序,其他的Project都要刪除App.xaml,輸出類型爲類庫。
二、刪除Desktop.MainWindow中的默認的MainWindow,新建一個Shell窗體,根據Prism的思想,這個Shell就是主窗體。
三、爲全部的Project添加Prism的引用,我這裏爲全部的Project安裝瞭如下四個Prism的Nuget包。
四、在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模塊同理。
五、添加一個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; //} } }
六、在App.xaml中把啓動項設置爲BootStrapper應該都會吧,不會的看源碼哦。至此,一個簡單的基於MEF的Prism框架就搭建好了。連接提供源碼。