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

Ash*_*non 16 c# wpf 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;
}
Run Code Online (Sandbox Code Playgroud)

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

我已经尝试将这个电话评论为Shutdown只是为了踢和笑,它仍然表现得一样.

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

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

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

Rus*_*ips 16

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

MessageBox.Show(
    "Message", 
    "Title",
    MessageBoxButton.YesNoCancel, 
    MessageBoxImage.Question, 
    MessageBoxResult.Cancel,
    MessageBoxOptions.DefaultDesktopOnly);
Run Code Online (Sandbox Code Playgroud)

  • 这是一种享受。最简单的解决方案。 (2认同)

Nei*_*eil 14

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

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


Ale*_*nov 8

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

  • 提供窗口所有者的重载不允许我传入null,但是这个答案解释了导致我的困境的原因.:) (2认同)
  • 这个答案是错误的,传递 null 会导致 `ArgumentNullException` (2认同)