强制MessageBox位于.net / WPF中的应用程序窗口之上

时间:2022-01-10 11:02:02

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.

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

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

我已经阅读了很多关于此问题的帖子,但没有人为我解决过这个问题。

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

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

3 个解决方案

#1


39  

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

使用带有Window“owner”并传递窗口的MessageBox.Show版本。

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

If your possibly not on the UI thread try:

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

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);
    }));
}

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).

您可以:1。调用阻塞您的线程,直到MessageBox被解除或者2. BeginInvoke,在这种情况下,您的线程代码将继续执行,但UI线程将在MessageBox上阻塞,直到它被解除)。

#2


1  

This is a quick way of putting the Message Box on top of the application windows.

这是将消息框置于应用程序窗口之上的快速方法。

MessageBox.Show(this ,"Output text"));

MessageBox.Show(this,“Output text”));

#3


0  

Inside your "public partial class MainWindow : Window" place the following code. So the Invoke will run your code inside UI thread.

在“公共部分类MainWindow:Window”中放置以下代码。因此Invoke将在UI线程中运行您的代码。

void ShowErrorMessage(ERROR err)
{
    this.Dispatcher.Invoke((Action)(() =>
    {
        MessageBox.Show(err.description, err.code.ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
    }));
}

#1


39  

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

使用带有Window“owner”并传递窗口的MessageBox.Show版本。

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

If your possibly not on the UI thread try:

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

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);
    }));
}

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).

您可以:1。调用阻塞您的线程,直到MessageBox被解除或者2. BeginInvoke,在这种情况下,您的线程代码将继续执行,但UI线程将在MessageBox上阻塞,直到它被解除)。

#2


1  

This is a quick way of putting the Message Box on top of the application windows.

这是将消息框置于应用程序窗口之上的快速方法。

MessageBox.Show(this ,"Output text"));

MessageBox.Show(this,“Output text”));

#3


0  

Inside your "public partial class MainWindow : Window" place the following code. So the Invoke will run your code inside UI thread.

在“公共部分类MainWindow:Window”中放置以下代码。因此Invoke将在UI线程中运行您的代码。

void ShowErrorMessage(ERROR err)
{
    this.Dispatcher.Invoke((Action)(() =>
    {
        MessageBox.Show(err.description, err.code.ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
    }));
}