如何将 Windows 表单元素主机的消息框显示为模态?



我在 C# 中有 Windows 表单应用程序。我有用于 WPF 用户控件的类库。我正在使用Element Host在Windows表单应用程序中使用它。现在在 WPF 用户控件中,我需要使用 MessageBox 显示一条消息。它正在工作,但在 Windows 窗体应用程序的主窗体上显示无模式。我想将此消息框显示为模型。

请帮忙。

编辑:

让我举例解释一下:我有"WindowsFormApp1"另一个是"WPFUserCtrlLib",它是类库。它的用户控件名为"WPFUserCtrl",所以我有"WPFUserCtrl.xaml"和"WPFUserCtrl.xaml.cs"文件。在WindowsFormApp1中,我有一个名为"Form1"的主窗体。在Form1中,我使用"WPFUserCtrl"使用Element Host。现在有一些逻辑驻留在"WPFUserCtrl.xaml.cs"说

String ErrorMsg=“Error There”;
if(condition)
{
//Do Task
}
else
{
MessageBox.Show(ErrorMsg);
}

所以在这里我需要显示错误Msg。当显示此消息框时,它是无模式的,因为我能够访问"Form1"上的控件,如菜单、按钮和所有。

是否将

WPF 窗口的所有者设置为 winforms 窗口?

根据此链接,为了使模态对话框在winforms中工作是必要的。

var window = new WpfWindow(); 
var helper = new WindowInteropHelper(window);
helper.Owner = this.Handle; 
window.ShowDialog();

我已经尝试了很多。无法获得解决方案,因此在此处发布了问题,并继续使用diff方法再次尝试。现在我找到了解决方案。

解决方案是:使用Dispatcher.Invoke

if(condition)
{
    //Do Task
}
else
{
    Dispatcher.Invoke(new Action(() => {
                MessageBox.Show(ErrorMsg);
            })
            );
}

现在我的消息框是模型。

最新更新