Friday 16 March 2012

My splash screen closed my message box

Today, while trying to debug on a very slow virtual machine, I decided to add a splash screen to my WPF application.  After all, my phone applications have one, so how hard could it be?

First I found these instructions on MSDN for adding a splash screen

Wow.  That’s easy!  However to my surprise, I found that in some circumstances, my application caused a message box to show before the main window was displayed, as my MessageBox was shown by the constructor.

And to my surprise, the splash screen closed that message box.  Now I’m not the first to find this problem.  There are various discussions and links here, but I have to admit, I didn’t really like any of the three options presented there.

So, I thought, there must be a better way.  Well, I think I’ve found one!

First, construct your SplashScreen object as shown in this constructor:

MainWindow()
{
SplashScreen ss = new SplashScreen("Images/splash.png");

// show the splash screen
ss.Show(false);

// do the rest of the constructor work.
// ...
//

ss.Close(new Timespan.FromSeconds(0.1));
}

 

When I first wrote the above, I had my splash screen flicker, as my main application appeared on top of the splash screen.  To solve that I came up with a very confusing BeginInvoke, which is only necessary to solve a different problem.

 

Turns out, that because I followed the initial instructions, I had added my splash screen with a build action of “SplashScreen”.  That’s wrong.  The build action should be resource if you want to do it manually, otherwise, it appears that you end up with two splash screens!


No comments:

Post a Comment