开发者

Is it possible to show a shell only once all the modules have been loaded?

开发者 https://www.devze.com 2023-04-09 23:56 出处:网络
I\'m currently working on an application which uses PRISM 4 to divide its functionalities in different modules.

I'm currently working on an application which uses PRISM 4 to divide its functionalities in different modules.

I noticed that my application's Shell, which holds the modules' views in its regions, is loaded and displayed before the modules are loaded.

This means that the Shell is displayed first, and a considerable amount of time later (about half a second), the modules are loaded and the views inserted into the Shell's regions. It's rather annoying, as the user is greeted with an empty shell at 开发者_如何学编程start-up, which isn't very professional.

Is there any way to detect when all the modules have been loaded ? Any method I can override in the bootstrapper ?

If I could, I would like to hide the Shell (or display a loading adorner) until all the modules have been loaded.


You can show your Shell view after Modules are initialized:

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

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


I found a relatively simple solution.

My application has a class named ShellHandler, which is instanciated in the bootstrapper and registered in the Unity Container as a singleton:

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

I created in my ShellHandler a method which can be used by modules to flag themselves as loaded:

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

Finally, in my ModuleBase class, which all modules implement, I created an abstract method which is called during the initialization process:

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

Each module now resolves the instance of the ShellHandler singleton through their constructor:

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

And finally they implement the Abstract method from 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();
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号