本文用一个非常简单的例子,讲述Prism中的启动工程boot strapper的逻辑。
首先看一下工程图:
这是启动类Boot strapper:
public class BootStrapper : UnityBootstrapper { /// <summary> /// 创建起始Window /// </summary> /// <returns></returns> protected override DependencyObject CreateShell() { var shell = this.Container.TryResolve<Shell>() as DependencyObject; return shell; } /// <summary> /// 与App.Current.MainWindow联系 /// </summary> protected override void InitializeShell() { base.InitializeShell(); App.Current.MainWindow = (Window)this.Shell; App.Current.MainWindow.Show(); } /// <summary> /// 配制ModuleCatalog,引用基类的一个this.ModuleCatalog /// </summary> protected override void ConfigureModuleCatalog() { base.ConfigureModuleCatalog(); ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog; moduleCatalog.AddModule(typeof(ModuleAManager)); } /// <summary> /// 配制ModuleCatalog,引用基类的一个this.ModuleCatalog /// </summary> /// <returns></returns> //protected override IModuleCatalog CreateModuleCatalog() //{ // return base.CreateModuleCatalog(); //} }
Shell.xaml:用RegionManager管理region中的内容:
<Window x:Class="BootStrapperDemo.Shell" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Regions="clr-namespace:Microsoft.Practices.Prism.Regions;assembly=Microsoft.Practices.Prism" Title="MainWindow" Height="350" Width="525"> <StackPanel> <ItemsControl Name="MainRegion" Regions:RegionManager.RegionName="MainRegion" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto" Width="Auto" > </ItemsControl> </StackPanel> </Window>
App.cs:
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { BootStrapper root = new BootStrapper(); root.Run(); } }
ModuleA.cs:
public class ModuleAManager:IModule { private readonly IRegionManager regionManager; public ModuleAManager(IRegionManager regionManager) { this.regionManager = regionManager; } public void Initialize() { // 可以在ItemsControl中注入多次 regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewTest)); regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewTest)); regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewTest)); } }
Demo代码下载:https://skydrive.live.com/#cid=6B286CBEF1610557&id=6B286CBEF1610557!694