C# WPF Tutorial - Using Splash Screens [Beginner]


Splash screens - everyone loves them, right? Actually, I'm not a big fan of them, but sometimes they are a necessary evil - especially in the world of WPF. A cold start up of a WPF application can be, well, slow - mostly because there are a lot of common WPF dlls that need to be loaded. Once they are loaded for the first time after boot up, WPF apps generally start pretty fast, but that first WPF app after a reboot can crunch away at your hard drive for quite a few seconds.

Adding a splash screen for WPF in the .NET 3.5 SP1 world was a huge pain. You had to pull in a couple native methods and essentially create your own window, and on top of all that there was a special build step that had to be executed. But now, it's so easy, it's almost not worth a tutorial (but I'm going to write it up anyway, cause hey, you never know).

Adding a splash screen for a WPF app has been turned into a three step process now that SP1 is out . The first step is to make the splash screen image. Break out Photoshop and do your worst! For the sample app here today, I just took a screen shot of the webpage and cropped out the top logo, so the splash screen image is going to look like this:

Image We Will Use As a Splash
Screen

OK, you have an image now? Good. Add it as an item to your WPF project. In our case, it is a png called SOTCSplash.png:

Visual Studio project listing with our image

And now for the final, horribly complicated step. Select that newly added image, go to the item properties, and for the Build Action property choose "SplashScreen":

Setting the build action on the image

Your done! That is all that you need to do now to create a splash screen for a WPF app. In the case of our example, the app looks like this when starting up:

A screenshot of the spash screen in action

The splash screen even does a nice automatic fade out.

Now, if you want a little more control over the splash screen, you can choose not to use that build action, and instead use the new SplashScreen class that comes in SP1. This is more complicated than the build action, but much less complicated than the old way. First, we have to add a Main method to App.xaml.cs. Generally, Visual Studio creates a main method automatically, but we don't want that to happen anymore, because we will be putting special code in that method. To do this, change the "Build Action" for App.xaml from "Application Definition" to "Page".

Now, in App.xaml.cs, we add a main method. The standard base main method for WPF looks something like this:
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
public static void Main()
{
  app.InitializeComponent();
  app.Run();
}
 
Now we want to add the splash screen to that. First, we need to set the build action for the image to "Resource", so that we can reference it. Then we add a couple lines to the main method:
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
public static void Main()
{
  SplashScreen splashScreen = new SplashScreen("SOTCSplash.png");
  splashScreen.Show(true);
  app.InitializeComponent();
  app.Run();
}
 
That will give the exact same behavior as using that "SplashScreen" build action from before. If we want to customize it a bit, we can do something like this:
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
public static void Main()
{
  SplashScreen splashScreen = new SplashScreen("SOTCSplash.png");
  splashScreen.Show(false);
  SplashScreenTest.App app = new SplashScreenTest.App();
  app.InitializeComponent();
  splashScreen.Close(TimeSpan.FromMilliseconds(1000));
  app.Run();
}
 
The boolean passed into the splash screen Show function determines if the splash screen will close automatically (true is automatic, false is manual). If we choose false, we have to remember to call Close at some point, but we also get the ability to say how long it takes for the splash screen to fade out. In this case, we are saying the fade out should take 1000 milliseconds (instead of the default 300).

And that is about it for the new stuff on splash screens that came with SP1 for .NET 3.5 and VS2008. You can check out the new SplashScreen class at MSDN. And just to leave on an amusing note here - Microsoft went to all this work to make splash screens easy in SP1, but their own User ExperienceGuidelines forVista recommend against using splash screens in the first place.

Comments