This isn’t anything special, but the documentation on Msdn for
System.Windows.SplashScreen is lacking. Basically I am writing a licensing system and during debugging the WPF stack can take many seconds to load. When making changes to this system sometimes it may result in a failure, but I may not see it for a few seconds. I wanted something to display during this time period so I know at least something is going on.
I knew that WPF has built-in support for splash screens by setting the Build Action of an image in my project to Splash Screen in the properties window, but the problem is that this only works for the project the image is embedded into, and only if its a WPF Application project. My requirements are that I can display the splash screen from my licensing dll (class library) when my licensing service is invoked. This means that the licensing dll would contain the image to display, and contain the logic to show the splash.
SplashScreen exposes a
SplashScreen(Assembly, String) constructor which allows you to do just this. To accomplish this task, all you need to do is add your image to your class library project, set its build action to Splash Screen, and then write three lines of code.
Assembly asm = Assembly.GetAssembly(GetType()); SplashScreen splash = new SplashScreen(asm, "SplashScreen.png"); splash.Show(true);
The first line is simple; we are just getting the assembly that contains the splash resource (in this case my licensing library). The second line takes the assembly as an argument, and we pass in the name of the image. Next we simply call
splash.Show(true); which actually displays the image, and because we passed in
true as the argument, it will automatically be closed by the WPF stack once the first window is constructed, loaded, and displayed.
Make sure that you add your image to the root of your project, so that the WPF stack can find it.
I haven’t looked into it because I don’t want to spend a lot of time on a task that doesn’t mean much, but placing the image at other hierarchical locations in your project structure can cause the embedded resource path string to change, and it may not be found by the WPF stack internally. It’s probably possible to specify a qualified path using the second constructor parameter, but I was not able to get it to work using a fully qualified resource path string.