how to set wpf MessageBox.Owner to desktop window because SplashScreen closes MessageBox

社会主义新天地 提交于 2019-11-29 04:06:22
dthrasher

Since using the desktop window as the parent for your modal dialogs is not a good idea, as @Nir pointed out in his answer, here are three other workarounds:

1) Use a hidden window. Create a tiny, non-modal window to act as the parent for your MessageBox or other modal dialog. This approach is described here:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/116bcd83-93bf-42f3-9bfe-da9e7de37546/

2) Create non-modal message windows. Change your startup mode to explicit shutdown and use a non-modal window to display your message. This approach is described in the answer to this StackOverflow question:

MessageBox with exception details immediately disappears if use splash screen in WPF 4.0

3) Call MessageBox twice. Apparently, the problem only affects the first modal dialog shown. So you could simply call your modal dialog twice, if you didn't mind the flash of the first one opening and closing.

https://connect.microsoft.com/VisualStudio/feedback/details/600197/wpf-splash-screen-dismisses-dialog-box

Personally, I don't like any of these workarounds. The only other option is to avoid the built-in SplashScreen functionality and to roll your own from scratch. Here's a link if you want to investigate that route further:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8dd49fd0-9cc9-43c6-b285-6f119ab8a32e/

Finally, if you're as annoyed by this issue as I am, you can vote for Microsoft to fix this bug here:

http://connect.microsoft.com/VisualStudio/feedback/details/600197/wpf-splash-screen-dismisses-dialog-box

I came up with this solution myself, so maybe there's something wrong with it, but it seems to work perfectly:

Window temp = new Window() { Visibility=Visibility.Hidden };
temp.Show();
MessageBox.Show(temp, "An error occurred before the application could start.\n\nTechnical Details: " + ex.Message, "Fatal Error", MessageBoxButton.OK, MessageBoxImage.Stop);
App.Current.Shutdown(1);
adriaanp

I found the problem. I am also using the build-in splash screen which causes this: WPF SplashScreen closes MessageBox

Can you post some code? I just tried adding this to the App.xaml.cs file in a new WPF application:

protected override void OnStartup(StartupEventArgs e)
{
    if (MessageBox.Show("Start app?", "Confirm Start", 
        MessageBoxButton.YesNo) == MessageBoxResult.No)
    {
        this.Shutdown();
        return;
    }

    this.StartupUri = new Uri("Window1.xaml", UriKind.Relative);
    base.OnStartup(e);
}

... and it works as expected (the "Confirm Start" prompt stays open until I've responded, and if I click "No" the app shuts down).

The desktop window is never the correct parent, read this to know why:

http://blogs.msdn.com/oldnewthing/archive/2004/02/24/79212.aspx

Now the problem described in this post doesn't happen so much because MS worked around it, in this post you can see how:

http://blogs.msdn.com/oldnewthing/archive/2006/11/02/931674.aspx

this has helped me a lot ..... Given me new idea but the example code that i have seen here has some modification required

here is an simple example in wpf with modification now it is working

on button click

paste this code

if (System.Windows.Forms.MessageBox.Show("are u sure", "delete", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { this.Close(); } else { MessageBox.Show("why not to delete"); }

This is not directly related to the OP's situation, but might be useful for others who are having problems with MessageBox being hidden behind other windows in certain special situations.

As @dthrasher mentions, one solution is to use a hidden dummy window. But sometimes even that is not enough. I had a situation where the solution was to not only use a hidden dummy window, but to turn on its TopMost property whenever I used it with MessageBox.

     _formKludge.TopMost = true;

     MessageBox.Show(_formKludge, "Nice informative message.", "Super-duper Program",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);

     _formKludge.TopMost = false;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!