【问题标题】:Is it possible to show a shell only once all the modules have been loaded?是否可以仅在加载所有模块后才显示外壳?
【发布时间】:2011-09-29 13:18:58
【问题描述】:

我目前正在开发一个使用 PRISM 4 将其功能划分为不同模块的应用程序。

我注意到我的应用程序的 Shell 在加载模块之前加载和显示,它在其区域中保存模块的视图。

这意味着首先显示Shell,并在相当长的一段时间后(大约半秒)加载模块并将视图插入Shell 的区域。这很烦人,因为用户在启动时收到一个空壳,这不是很专业。

有什么方法可以检测所有模块何时加载?我可以在引导程序中覆盖任何方法吗?

如果可以的话,我想隐藏 Shell(或显示加载装饰器),直到所有模块都加载完毕。

【问题讨论】:

  • 我不认为会做你需要的,但你看过 SplashScreen 吗?
  • 我不知道 SplashScreen,我会调查一下,谢谢。我不确定这是否能解决问题,因为我仍然需要确定模块何时完成加载。

标签: c# .net wpf prism


【解决方案1】:

您可以在模块初始化后显示您的 Shell 视图:

protected override void InitializeShell()
{
   Application.Current.MainWindow = (Window)Container.Resolve<ShellView>();
}

protected override void InitializeModules()
{
   base.InitializeModules();
   Application.Current.MainWindow.Show();
}

【讨论】:

    【解决方案2】:

    我找到了一个相对简单的解决方案。

    我的应用程序有一个名为 ShellHandler 的类,它在引导程序中实例化并在 Unity 容器中注册为单例:

    Container.RegisterType<IShellHandler, ShellHandler>(new ContainerControlledLifetimeManager());
    

    我在我的 ShellHandler 中创建了一个方法,模块可以使用该方法将自己标记为已加载:

     /// <summary>
     /// Method used to increment the number of modules loaded.
     /// Once the number of modules loaded equals the number of modules registered in the catalog,
     /// the shell displays the Login shell.
     /// This prevents the scenario where the Shell is displayed at start-up with empty regions,
     /// and then the regions are populated as the modules are loaded.
     /// </summary>
     public void FlagModuleAsLoaded()
     {
         NumberOfLoadedModules++;
    
         if (NumberOfLoadedModules != ModuleCatalog.Modules.Count()) 
             return;
    
         // Display the Login shell.
         DisplayShell(typeof(LoginShell), true);
     }
    

    最后,在所有模块都实现的 ModuleBase 类中,我创建了一个在初始化过程中调用的抽象方法:

     /// <summary>
     /// Method automatically called and used to register the module's views, types, 
     /// as well as initialize the module itself.
     /// </summary>
     public void Initialize()
     {
         // Views and types must be registered first.
         RegisterViewsAndTypes();
    
         // Now initialize the module.
         InitializeModule();
    
         // Flag the module as loaded.
         FlagModuleAsLoaded();
     }
    
     public abstract void FlagModuleAsLoaded();
    

    现在每个模块都通过它们的构造函数解析 ShellHandler 单例的实例:

     public LoginModule(IUnityContainer container, IRegionManager regionManager, IShellHandler shellHandler) 
            : base(container, regionManager)
     {
          this.ShellHandler = shellHandler;
     }
    

    最后他们实现了 ModuleBase 中的 Abstract 方法:

     /// <summary>
     /// Method used to alert the Shell Handler that a new module has been loaded.
     /// Used by the Shell Handler to determine when all modules have been loaded
     /// and the Login shell can be displayed.
     /// </summary>
     public override void FlagModuleAsLoaded()
     {
         ShellHandler.FlagModuleAsLoaded();
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-11
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2017-07-23
      相关资源
      最近更新 更多