...and it makes no sense why. T-T
In my Application_Startup
event handler I have code that looks kinda like this:
private void Applicati
Try to use an overload that accepts System.Windows.Window
parameter and pass Null
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.
Based on Alexey Ivanov's suggestion, I successfully used a new window as the parent
System.Windows.Forms.MessageBox.Show(new System.Windows.Forms.NativeWindow(), errorMessage, "Application Startup", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
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):
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
ShutdownMode="OnExplicitShutdown">
</Application>
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.
Create transparent hidden window and use it as an owner of the MessageBox:
private Window CreateHiddenWindow()
{
var window = new Window
{
AllowsTransparency = true,
Background = System.Windows.Media.Brushes.Transparent,
WindowStyle = WindowStyle.None,
Top = 0,
Left = 0,
Width = 1,
Height = 1,
ShowInTaskbar = false
};
window.Show();
return window;
}
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:
var splashScreen = new SplashScreen("path/to/splash_image.bmp");
splashScreen.Show(false); //make sure to use 'false' here to prevent auto-closing
string errorMessage;
if(CheckStartUpConditions(out errorMessage))
{
(new MainWindow()).Show();
}
else
{
//standard WPF MessageBox may now be used here
MessageBox.Show(errorMessage, "Application Startup",
MessageBoxButton.OK, MessageBoxImage.Error);
Shutdown();
}
//explicitly close the splash screen now
splashScreen.Close(TimeSpan.FromSeconds(1));
Alexey is right, the splash screen closes the message box.
A simple way to avoid this is to use the native MessageBox function:
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
public static void Main()
{
...
MessageBox(new IntPtr(0), "Hello World!", "MyApp", 0);