WPF MessageBox not waiting for result [WPF NotifyIcon]

非 Y 不嫁゛ 提交于 2019-12-05 04:57:13

You don't need to create a proxy window for this. Just add MessageBoxOptions.DefaultDesktopOnly to your message box and it will fire on your desktop without disappearing.

Example

MessageBox.Show("My Message", "Title", MessageBoxButton.OK, 
    MessageBoxImage.Information, MessageBoxResult.OK, 
    MessageBoxOptions.DefaultDesktopOnly);
Gigi

According to the answer here, a workaround is to actually open an invisible window and use that as the parent of the MessageBox:

        Window window = new Window()
        {
            Visibility = Visibility.Hidden,
            // Just hiding the window is not sufficient, as it still temporarily pops up the first time. Therefore, make it transparent.
            AllowsTransparency = true,
            Background = System.Windows.Media.Brushes.Transparent,
            WindowStyle = WindowStyle.None,
            ShowInTaskbar = false
        };

        window.Show();

...then open the MessageBox with the appropriate parameter:

        MessageBox.Show(window, "Titie", "Text");

...and don't forget to close the window when you're done (possibly on application exit):

        window.close();

I tried this and it works well. It's undesirable to have to open an extra window, but it's better than making your own messagebox window just for the sake of making this work.

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