是否可能仅在加载所有模块后才显示shell ?

本文关键字:模块 显示 shell 加载 是否 | 更新日期: 2023-09-27 18:09:58

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

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

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

是否有任何方法可以检测所有模块何时已加载?我可以在引导程序中重写任何方法?

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

是否可能仅在加载所有模块后才显示shell ?

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

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

我找到了一个相对简单的解决办法。

我的应用程序有一个名为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实现抽象方法:

 /// <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();
 }