第531篇--Prism Boot Strapper Demo

对着背影说爱祢 提交于 2019-11-26 21:34:07

本文用一个非常简单的例子,讲述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

转载于:https://www.cnblogs.com/shanghaijimzhou/archive/2013/04/05/3000612.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!