强制 MessageBox 位于 .net/WPF 中的应用程序窗口顶部 [英] Force MessageBox to be on top of application window in .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
被解除 OR
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屋!