Prism之WPF模块化入门(一)
本文参考微软文档:https://msdn.microsoft.com/en-us/library/ff921141(v=pandp.40).aspx ,步骤与原文不一一对应。希望能帮助做C/S程序(WPF)的小伙伴们。
简单的介绍一下Prism,Prism的关注点是分离和松散耦合。Prism可帮助我们设计和构建具有重要表现和业务逻辑的应用程序。预计应用程序将在其生命周期内发生显着变化,以响应新的需求和业务机会。Prism构建的应用程序是“为了持久”而建立的,并且是为了改变而建立的。不需要这些特性的应用程序可能不会从使用Prism中受益。
⒈建立一个空白的解决方案命名为“HelloWorld.Desktop”。
⒉添加一个“新建项目”>“WPF应用”>命名为“HelloWorld.Desktop”。
⒊ 通过“管理NuGet程序包”,添加“Unity for Prism 6”、“Prism 6”、“Prism 6 for WPF”,如下图所示。
添加完成之后,引入了如下dll,如下图所示
⒋将“MainWindow.xaml”重命名为“Shell”
<Window x:Class="HelloWorld.Desktop.Shell" xmlns:prism="http://www.codeplex.com/prism" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Hello World" Height="300" Width="300"> <Grid> <ItemsControl x:Name="MainRegion" prism:RegionManager.RegionName="MainRegion"/> </Grid> </Window>
⒌修改“App.xaml.cs”中的代码,添加代码如下
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); Bootstrapper bootstrapper=new Bootstrapper(); bootstrapper.Run(); } }
删除 StartupUri="MainWindow.xaml"
<Application x:Class="HelloWorld.Desktop.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> <!--StartupUri="MainWindow.xaml"--> </Application.Resources> </Application>
⒍添加“Bootstrapper.cs” 类,代码如下:
public class Bootstrapper:UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return new 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(HelloWorldModule.HelloWorldModule));
}
}
⒎添加新项目类库“HelloWorldModule”。
⒏添加文件夹“Services”、“ViewModels”、“Views”。
⒐在文件夹“Views”中添加用户控件“HelloWorldView.xaml”。
<UserControl x:Class="HelloWorldModule.Views.HelloWorldView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <TextBlock Text="Helllo World" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"/> </Grid> </UserControl>
⒑添加模块类“HelloWorldModule.cs”。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Prism; using Prism.Modularity; using Prism.Regions; namespace HelloWorldModule { public class HelloWorldModule : IModule { private readonly IRegionManager regionManager; public void Initialize() { regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView)); } public HelloWorldModule(IRegionManager regionManager) { this.regionManager = regionManager; } } }