Prism应用开发(二)——Prism应用程序初始化

Bootstrapper主要用来初始化Prism应用程序,其处理流程如图:

在Prism应用中,Bootstrapper的开发过程如下:

override基类的Bootstrapper

Unity基类提供了UnityBootstrapper和MefBootstrapper,可以根据实际的应用选用不同的Bootstrapper。

  1. class DirectorBootstrapper : UnityBootstrapper  
  2.     {  
  3.         protected override System.Windows.DependencyObject CreateShell()  
  4.         {  
  5.             return Container.Resolve<Shell>();   
  6.         }  
  7.   
  8.         protected override void InitializeShell()  
  9.         {  
  10.             Application.Current.MainWindow = (Window)Shell;  
  11.             Application.Current.MainWindow.Show();   
  12.         }  
  13.     }  
class DirectorBootstrapper : UnityBootstrapper
    {
        protected override System.Windows.DependencyObject CreateShell()
        {
            return Container.Resolve<Shell>(); 
        }

        protected override void InitializeShell()
        {
            Application.Current.MainWindow = (Window)Shell;
            Application.Current.MainWindow.Show(); 
        }
    }
在App类中调用Bootstrapper的Run函数

  1. public partial class App : Application  
  2.     {  
  3.         protected override void OnStartup(StartupEventArgs e)  
  4.         {  
  5.             base.OnStartup(e);  
  6.   
  7.             DirectorBootstrapper bootstrapper = new DirectorBootstrapper();  
  8.             bootstrapper.Run();   
  9.         }  
  10.     }  
public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            DirectorBootstrapper bootstrapper = new DirectorBootstrapper();
            bootstrapper.Run(); 
        }
    }
配置ModuleCatalog
下面的代码使用plug in模式,将Module配置在Shell中。

  1. protected override IModuleCatalog CreateModuleCatalog()  
  2.         {  
  3.             ModuleCatalog catalog = new ConfigurationModuleCatalog();  
  4.             return catalog;  
  5.         }  
protected override IModuleCatalog CreateModuleCatalog()
        {
            ModuleCatalog catalog = new ConfigurationModuleCatalog();
            return catalog;
        }

  1. <configSections>  
  2.     <section name="modules" type="Microsoft.Practices.Prism.Modularity.ModulesConfigurationSection, Microsoft.Practices.Prism"/>  
  3.   </configSections>  
  4.   
  5.   <modules>  
  6.     <module assemblyFile="Modules.DeviceManager.dll"   
  7.             moduleType="Modules.DeviceManager.DeviceManagerModule, Modules.DeviceManager"   
  8.             moduleName="DeviceManagerModule"/>  
  9.   </modules>  
<configSections>
    <section name="modules" type="Microsoft.Practices.Prism.Modularity.ModulesConfigurationSection, Microsoft.Practices.Prism"/>
  </configSections>

  <modules>
    <module assemblyFile="Modules.DeviceManager.dll" 
            moduleType="Modules.DeviceManager.DeviceManagerModule, Modules.DeviceManager" 
            moduleName="DeviceManagerModule"/>
  </modules>
实现模块接口
  1. public class DeviceManagerModule : IModule  
  2.     {  
  3.         private IUnityContainer unityContainer;  
  4.         private IRegionManager regionManager;   
  5.   
  6.         public DeviceManagerModule(IUnityContainer container, IRegionManager regionManager)  
  7.         {  
  8.             this.unityContainer = container;  
  9.             this.regionManager = regionManager;   
  10.         }  
  11.  
  12.         #region IModule Members  
  13.   
  14.         /// <summary>  
  15.         /// IModule interface method called on module loading.  
  16.         /// </summary>  
  17.         public void Initialize()  
  18.         {  
  19.             this.regionManager.RegisterViewWithRegion(RegionNames.MainRegion,  
  20.                 () => this.unityContainer.Resolve<DeviceView>("DeviceView"));   
  21.         }  
  22.  
  23.         #endregion  
  24.     }  
public class DeviceManagerModule : IModule
    {
        private IUnityContainer unityContainer;
        private IRegionManager regionManager; 

        public DeviceManagerModule(IUnityContainer container, IRegionManager regionManager)
        {
            this.unityContainer = container;
            this.regionManager = regionManager; 
        }

        #region IModule Members

        /// <summary>
        /// IModule interface method called on module loading.
        /// </summary>
        public void Initialize()
        {
            this.regionManager.RegisterViewWithRegion(RegionNames.MainRegion,
                () => this.unityContainer.Resolve<DeviceView>("DeviceView")); 
        }

        #endregion
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值