使用SplashScreen显示的WPF MessageBox的非模态行为 - bug?

时间:2014-09-03 16:15:59

标签: c# .net wpf xaml splash-screen

假设我们有以下代码:

    void App_Startup(object sender, StartupEventArgs args)
    {
        MessageBox.Show("something");
    }
应用程序启动时调用的

    <Application x:Class="AppClass.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 Startup="App_Startup"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
        </Application.Resources>
    </Application>

当我在没有SplashScreen的情况下运行它时,它会按预期运行(即等待点击OK),但是当与SplashScreen一起运行时,消息框会与{{1}一起消失}}。

这是正常行为还是错误?

2 个答案:

答案 0 :(得分:3)

如果您显示MessageBox并且没有明确设置父级,则会发生这种情况。该窗口将隐式地将其自身从任何当前活动的窗口(在这种情况下,启动屏幕)中移除。在Win32世界中,如果关闭窗口,则所有子窗口也将关闭。如果您明确地将MessageBox的父级设置为另一个窗口,那么您就可以了。

已向微软here报告此问题,并已讨论了可能的补救措施here

答案 1 :(得分:2)

试试这个,

SplashScreen Show的第二个参数(false,false)方法是指定splashscreen是否应该是最顶层的。

void App_Startup(object sender, StartupEventArgs args)
{
   SplashScreen screen = new SplashScreen("SplashImage.png");
   screen.Show(false, false);

   MessageBox.Show("something");

   splashScreen.Close(TimeSpan.FromSeconds(1));
}
相关问题