【转】WPF PRISM开发入门一( 初始化PRISM WPF程序)
這篇博客將介紹在WPF項目中引入PRISM框架進行開發的一些基礎知識。目前最新的PRISM的版本是Prism 6.1.0,可以在Github上獲取PRISM的源碼。這個系列的博客將選擇PRISM 4.1版本來講解。可以從微軟官網上下載到PRISM 4.1相關內容。將下載下來的文件解壓開:
新建一個WPF解決方案如下:
解決方案中包含兩個工程,GetStartedPrismWPF是一個WPF項目,GetStartedPrismWPF.MainModule是一個類庫項目。這兩個項目中都同時添加下面Prism相關的Dll,
在GetStartedPrismWPF.MainModule類庫中需要額外引用PresentationCore,PresentationFramework,ReachFramework,System.Xaml這4個WPF相關的類庫。
GetStartedPrismWPF工程中刪除MainWindow.xaml文件,新建一個Shell.xaml窗體文件,XAML代碼如下:
<Window x:Class="GetStartedPrismWPF.Shell"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:prism="http://www.codeplex.com/prism"xmlns:local="clr-namespace:GetStartedPrismWPF"mc:Ignorable="d"Title="GetStarted Prism for WPF" Height="300" Width="300"><Grid><ContentControl prism:RegionManager.RegionName="MainRegion"/></Grid> </Window>添加一個PrismGetStartedBootstrapper類,代碼如下:
using Microsoft.Practices.Prism.Modularity; using Microsoft.Practices.Prism.UnityExtensions; using System.Windows;namespace GetStartedPrismWPF {public class PrismGetStartedBootstrapper : UnityBootstrapper{protected override DependencyObject CreateShell(){return this.Container.TryResolve<Shell>();}protected override void InitializeShell(){base.InitializeShell();Application.Current.MainWindow = (Window)this.Shell;Application.Current.MainWindow.Show();}protected override void ConfigureModuleCatalog(){base.ConfigureModuleCatalog();ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;moduleCatalog.AddModule(typeof(MainModule.GetStartedPrismWPFMainModule));}} }打開App.xaml文件,刪除StartupUri的代碼,在后臺代碼中添加:
protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);PrismGetStartedBootstrapper bootstrapper = new PrismGetStartedBootstrapper();bootstrapper.Run();}上面兩段代碼的意思是將主窗體設置為Shell窗體。
下面看GetStartedPrismWPF.MainModule中的代碼,新建一個UserControl,命名為GetStartedPrismView,XAML代碼如下:
很簡單,就一句話顯示一段文字。新建一個GetStartedPrismWPFMainModule,集成自IModule,
public class GetStartedPrismWPFMainModule : IModule{private readonly IRegionViewRegistry regionViewRegistry;public GetStartedPrismWPFMainModule(IRegionViewRegistry registry){this.regionViewRegistry = registry;}public void Initialize(){regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Views.GetStartedPrismView));}}這里會把GetStartedPrismView這個UserControl注冊到MainRegion這樣一個占位符上,而這個占位符在WPF工程中Shell窗體XAML代碼中出現過,在Shell中,我們先定義好這樣一個占位符,后續可以對他填充Module。
此時運行這個項目,運行效果如下:
這個時候我們來對PRISM的基礎架構做一個簡單的描述。
Shell: 頂級窗口,就像一個空蕩蕩的舞臺,Shell本身不包含內容,他的功能都是通過Module來實現的;
?
Bootstrapper: 應用程序的入口點,就像一個工廠的調度,需要完成很多協調事情;
?
Region: 內容區域,類似于一個占位符,先把坑占了,至于上面的蹲坑的人是可以換的;
?
Module: 真正實現業務功能的東西,是View,數據,模型組成的集合,一個應用程序中可以根據復雜程度分很多Module;
?
用一個電影院來舉例子:Shell就是一個劇院,里面空空蕩蕩的演出廳就是Region,Bootstrapper就是劇場運營部門,安排演出單位的引入和演出安排及演出廳之間的資源協調;Module就是各個演出劇。
?
感謝您的閱讀!代碼點擊這里下載。
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
https://www.cnblogs.com/yang-fei/p/5193886.html?spm=a2c6h.12873639.0.0.75796827p2wRvf
總結
以上是生活随笔為你收集整理的【转】WPF PRISM开发入门一( 初始化PRISM WPF程序)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大专学历办信用卡多少额度
- 下一篇: Azure Table storage