在进入poof之前,System.Windows.MessageBox不等待用户输入!

……这没有任何意义。 TT

在我的Application_Startup事件处理程序中,我的代码看起来有点像这样:

 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; } 

在进入“POOF!”之前,消息框会短暂出现一段时间。 它不等我单击“确定”或“X”按钮。 我真的很难过为什么会这样,所以任何帮助都会非常感激

我已经尝试将对Shutdown的调用评论为踢和笑,但它的行为方式仍然相同。

此外,该应用程序还有一个SplashScreen ,所以我不知道这是否会影响这一点。

编辑:如果有帮助,我添加了更多代码。 消息框显示正确的错误消息。 只是不会留足够长的时间让用户阅读它。 > 🙁

编辑第2部分:好的……我想我找到了罪魁祸首。 :(我更改了我正在使用的图像上的构建操作,因为我从SplashScreen到我的飞溅到无,并且消息框现在将保留并等待用户输入。我不明白为什么SplashScreen与MessageBox一起拧紧。>: (

尝试使用接受System.Windows.Window参数的重载并传递Null值,以使MessageBox成为应用程序的顶级窗口,该窗口独立于可能存在的所有其他窗口。 我猜你的MessageBox由splashscreen表单拥有。 当关闭splashscreen时,框架会关闭MessageBox。 所以让你的MessageBox无主,应该做到这一点。

基于Alexey Ivanov的建议,我成功地使用了一个新窗口作为父窗口

 System.Windows.Forms.MessageBox.Show(new System.Windows.Forms.NativeWindow(), errorMessage, "Application Startup", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 

消息框立即消失,因为它没有所有者。 如果指定选项MessageBoxOptions.DefaultDesktopOnly ,则桌面将被指定为所有者,并且消息框将在没有主窗口的应用程序上正常工作。

 MessageBox.Show( "Message", "Title", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Cancel, MessageBoxOptions.DefaultDesktopOnly); 

创建透明隐藏窗口并将其用作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; } 

如果在代码中实现启动屏幕而不是图像构建操作,则可以保留启动画面并使用标准WPF MessageBox。

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是对的,启动画面关闭了消息框。

避免这种情况的一种简单方法是使用本机MessageBox函数:

 [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); 

您需要告诉WPF在第一个窗口时不要关闭。 看看这个: http : //msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx 。 您希望将应用程序设置为显式关闭(而不是在第一个窗口关闭后):

   

您的另一个选择是将MainWindow设置为StartupUri(而不是启动画面),然后在MainWindow加载的事件中加载启动画面。 然后你会做你需要做的任何加载或时间密集的事情,隐藏你的启动画面,并重新显示你的主窗口。