...and it makes no sense why. T-T
In my Application_Startup
event handler I have code that looks kinda like this:
private void Application_Startup(object sender, StartupEventArgs e)
{
string errorMessage;
if(CheckStartUpConditions(out errorMessage))
{
(new MainWindow()).Show();
}
else
{
MessageBox.Show(errorMessage, "Application Startup",
MessageBoxButton.OK, MessageBoxImage.Error);
Shutdown();
}
}
private bool CheckStartUpConditions(out string errorMessage)
{
errorMessage = string.Empty;
if(...)
errorMessage += "Please login to xxx. ";
if(...)
errorMessage += "Please install xxx.";
if(string.IsNullOrEmpty(errorMessage))
return true;
else
return false;
}
The message box makes an brief appearance for like a second before going "POOF!" It doesn't wait for me to click "OK" or on the "X" button. I'm really stumped as to why this is occuring, so any help would be greatly appreciated.
I've tried commenting out the call to Shutdown
just for kicks and giggles, and it still behaves the same way.
Also, the application also has a SplashScreen
, so I don't know if that's effecting this.
EDIT: I added more code if that helps. The message box is showing the correct error message. Just won't stay long enough for the users to read it. >:(
EDIT PART 2: Okay...I think I've found the culprit. :( I changed the build action on the image I'm using as my splash from SplashScreen to None and the message box will now stay and wait for user input. I don't understand why the SplashScreen is screwing with the MessageBox. >:(
Create transparent hidden window and use it as an owner of the MessageBox:
You need to tell WPF not to shutdown when the first window does. Look at this: http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx. You want to set your application to shutdown explicitly (instead of after the first window closes):
Your other choose is to set your MainWindow as the StartupUri (instead of your splash screen) and then load your splash screen in the MainWindow's loaded event. Then you would do whatever loading or time intensive stuff you need to do, hide your splash screen, and re-show your main Window.
The message box vanishes immediately because it has no owner. If you specify the option
MessageBoxOptions.DefaultDesktopOnly
, the desktop will be assigned as the owner, and the message box will work correctly on an application with no main window.Based on Alexey Ivanov's suggestion, I successfully used a new window as the parent
Try to use an overload that accepts
System.Windows.Window
parameter and passNull
value to make your MessageBox a top-level window of your application, which is independent of all other windows that may exist. I guess your MessageBox gets owned by splashscreen form. When splashscreen is closed, the framework closes the MessageBox. So making your MessageBox ownerless should do the trick.You can keep your splash screen and use a standard WPF MessageBox if you implement the splash screen in code instead of as an image build action.
App.xaml.cs: