Splash Screen with Prism
The other day, I was working on the WPF application that I’m spending my spare time with. I’ve been trying to make improvements to startup time of the application’s cold-start. With all the improvements done, I thought it’d be a good idea to add a splash screen and show the user about the loading progress. It is relatively easy to setup a splash screen for a WPF application and in fact, I’ve done that before. But in this application I’m using Prism framework, which if you do not already know, is a WPF Composite Application framework.
Prism itself has some initialization procedure which takes place when you run the bootstrap. Those are :
- Create Prism logger facility (ILoggerFacade implementation)
- Create the IoC container (Unity, Windsor, etc.)
- Register base services like Module Enumerator, Event Aggregator, Region Manager, etc.
- Configuration of Region Manager control for base controls like Selector, ContentControl etc.
- Creating the Shell Window
- Module initializations
…which you usually add other initializations like services, registering custom control region managers, loading resources, etc. and even if you don’t, steps 5 and 6 could take long enough, specially if you have multiple Modules or a complex Shell window. So, all this tells me we need a splash window. I also needed to give some kind of feedback on the application initialization process and the splash window should be a WPF window so that I can benefit the power of WPF. Thanks to performance optimizations made in .NET FX 3.5 SP 1, it would not take much to load WPF related libraries in case of a cold-start, so using a WPF splash screen may not be such a bad idea. Also, to spice it up, we’ll add an image for every modules we’re loading to the splash screen.
Supporting the Splash Screen
To add the support of showing and hiding the splash screen, I’ve created an interface that needs to be implemented by our application. Our application needs to implement this interface :
1 | /// <summary> |
Creating a Bootstrapper
I’ve been using Windsor container as my primary IoC container. I’ve also developed whole adapter which soon will be available on CompositeWPF Contrib project, which allows you work with Prism and Windsor.
If you’re using Unity, you should have no problem. To show our splash screen, we’ll override the Run method of the Bootstrapper, and first display the splash screen, and then let the base implementation do its job :
1 | private ISplashApplication splashOwner; |
Loading Modules
Prism uses a default implementation of IModuleLoader to create and initialize IModule instances found by IModuleEnumerator. Since we need to update our splash screen before a module is created and initialized, we need to plug-in our own implementation of IModuleLoader which exposes events before initializing modules:
1 | public class SplashModuleLoader : ModuleLoader |
…and we’ll listen to this event in the bootstrapper and show the message / image for the IModule being loaded :
1 | protected override void InitializeModules() |
Splash Screen
As said before the Splash Screen is actually a borderless WPF Window, displaying a image in the background plus bunch of Labels to display loading messages and a ListBox to show module images. To show module’s images, I’ve used a horizontal ListBox with images added as list box items.
1 | <Grid> |
…and when the splash window is displayed with some modules loaded, it should be something like this:
Hope you like it. ~ You can download the source files from here. I’d appreciate your feedbacks and comments about it.
Updated: Download the source file from here