强制MessageBox在.net/WPF中的应用程序窗口顶部 [英] Force MessageBox to be on top of application window in .net/WPF

查看:221
本文介绍了强制MessageBox在.net/WPF中的应用程序窗口顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序中,有时我处于System.Windows.MessageBox状态.最初显示时,它会显示在我的主应用程序窗口的顶部,如我所愿.有没有一种方法可以强制我始终将其保留在主窗口的顶部?我的问题是,当显示MessageBox时,用户可以在主应用程序窗口上单击并将其放在最前面,这意味着MessageBox从视图中隐藏起来.在这种情况下,用户可能没有意识到它在那里,或者忘记了它,对他们而言,主应用似乎已冻结.

In my WPF app, I sometimes being up a System.Windows.MessageBox. When it is initially displayed, it is shown on top of my main application window, as I would like. Is there a way that I can force it to ALWAYS remain top of the main window? The problem I have is that when a MessageBox is displayed, users can then click on the main app window and bring it to the front, meaning the MessageBox becomes hidden from view. In this case the user might not realize it's there, or forget about it, and to them, the main app seems to have frozen.

我已经阅读了许多有关此主题的主题,但是没有一个主题可以为我解决问题.

I've read a number of threads about this, but none have resolved the problem for me.

我应该补充一点,放置MessageBox的线程可能不是UI线程. 谢谢 汤姆

I ought to add that the thread putting up the MessageBox might not be the UI thread. Thanks Tom

推荐答案

使用带有窗口所有者"并通过您的窗口的MessageBox.Show版本.

Use the version of MessageBox.Show that takes a Window "owner" and pass your window.

MessageBox.Show(Application.Current.MainWindow, "Im always on top - of the main window");

如果您可能不在UI线程上,请尝试:

If your possibly not on the UI thread try:

string msg="Hello!";
if (Application.Current.Dispatcher.CheckAccess()) {
    MessageBox.Show(Application.Current.MainWindow, msg);
}
else {
    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(()=>{
        MessageBox.Show(Application.Current.MainWindow, msg);
    }));
}

您可以:
1. Invoke阻止您的线程,直到关闭MessageBox
2. BeginInvoke,在这种情况下,您的线程代码将继续执行,但UI线程将在MessageBox上阻塞,直到被关闭为止).

You can:
1. Invoke to block your thread until MessageBox is dismissed OR
2. BeginInvoke in which case your thread code will continue to execute but UI thread will block on MessageBox until its dismissed).

这篇关于强制MessageBox在.net/WPF中的应用程序窗口顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆