1、创建WPF应用程序Modules,创建类库ModuleA,向Modules中添加对ModuleA的引用。
2、在Modules中,将App.xaml中的StartupUri="MainWindow.xaml"删除。
3、在Modules中,使用NuGet安装Prism.Wpf、Prism.Core、Prism.Unity。
4、在Modules中,添加类“Bootstrapper”,编辑如下:
1 using Microsoft.Practices.Unity; 2 using Prism.Unity; 3 using Modules.Views; 4 using System.Windows; 5 using Prism.Modularity; 6 using ModuleA; 7 8 namespace Modules 9 { 10 class Bootstrapper : UnityBootstrapper 11 { 12 protected override DependencyObject CreateShell() 13 { 14 return Container.Resolve<MainWindow>(); 15 } 16 17 protected override void InitializeShell() 18 { 19 Application.Current.MainWindow.Show(); 20 } 21 22 protected override void ConfigureModuleCatalog() 23 { 24 var moduleAType = typeof(ModuleAModule); 25 ModuleCatalog.AddModule(new ModuleInfo() 26 { 27 ModuleName = moduleAType.Name, 28 ModuleType = moduleAType.AssemblyQualifiedName, 29 InitializationMode = InitializationMode.OnDemand 30 }); 31 } 32 } 33 }
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 <startup> 4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 5 </startup> 6 </configuration>