1、创建WPF应用程序Modules,创建类库ModuleA。
2、在Modules中,将App.xaml中的StartupUri="MainWindow.xaml"删除。
3、在Modules中,使用NuGet安装Prism.Wpf、Prism.Core、Prism.Unity。
4、在Modules中,添加类“Bootstrapper”,编辑如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using Modules.Views; 8 using Prism.Unity; 9 using Microsoft.Practices.Unity; 10 using Prism.Modularity; 11 12 namespace Modules 13 { 14 class BootStarpper:UnityBootstrapper 15 { 16 protected override DependencyObject CreateShell() 17 { 18 return Container.Resolve<MainWindow>(); 19 } 20 21 protected override void InitializeShell() 22 { 23 Application.Current.MainWindow.Show(); 24 } 25 26 protected override IModuleCatalog CreateModuleCatalog() 27 { 28 return new ConfigurationModuleCatalog(); 29 } 30 } 31 }
5、MainWindow.xaml代码如下:
1 <Window x:Class="Modules.Views.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:Modules" 7 xmlns:prism="http://prismlibrary.com/" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="450" Width="800"> 10 <Grid> 11 <ContentControl prism:RegionManager.RegionName="ContentRegion"/> 12 </Grid> 13 </Window>
6、修改App.xaml。
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Data; 5 using System.Linq; 6 using System.Threading.Tasks; 7 using System.Windows; 8 9 namespace Modules 10 { 11 /// <summary> 12 /// App.xaml 的交互逻辑 13 /// </summary> 14 public partial class App : Application 15 { 16 protected override void OnStartup(StartupEventArgs e) 17 { 18 base.OnStartup(e); 19 20 var bootStarpper=new BootStarpper(); 21 bootStarpper.Run(); 22 } 23 } 24 }
7、在ModuleA中,创建ModuleAModule.cs,代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using ModuleA.Views; 7 using Prism.Modularity; 8 using Prism.Regions; 9 10 namespace ModuleA 11 { 12 public class ModuleAModule:IModule 13 { 14 IRegionManager _regionManager; 15 16 public ModuleAModule(RegionManager regionManager) 17 { 18 _regionManager = regionManager; 19 } 20 21 public void Initialize() 22 { 23 _regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA)); 24 } 25 } 26 }
8、ViewA.xaml
1 <UserControl x:Class="ModuleA.Views.ViewA" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:local="clr-namespace:ModuleA.Views" 7 mc:Ignorable="d" 8 d:DesignHeight="450" d:DesignWidth="800"> 9 <Grid> 10 <TextBlock Text="View A" FontSize="38"/> 11 </Grid> 12 </UserControl>
9、Modules中修改App.config文件,代码如下:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <section name="modules" type="Prism.Modularity.ModulesConfigurationSection, Prism.Wpf" /> 5 </configSections> 6 <startup> 7 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 8 </startup> 9 <modules> 10 <module assemblyFile="ModuleA.dll" moduleType="ModuleA.ModuleAModule, ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleAModule" startupLoaded="True" /> 11 </modules> 12 </configuration>