Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何显示Windows窗体元素主机';s消息框是否为模态?_C#_Wpf_Winforms_Messagebox_Elementhost - Fatal编程技术网

C# 如何显示Windows窗体元素主机';s消息框是否为模态?

C# 如何显示Windows窗体元素主机';s消息框是否为模态?,c#,wpf,winforms,messagebox,elementhost,C#,Wpf,Winforms,Messagebox,Elementhost,我用C#编写了windows窗体应用程序。 我有WPF用户控制的类库。我正在Windows窗体应用程序中使用Element Host。现在在WPF用户控件中,我需要使用MessageBox显示一条消息。它正在工作,但在Windows窗体应用程序的主窗体上显示为非模态。我想将此消息框显示为模型 请帮忙 编辑: 让我举例说明: 我有“WindowsFormApp1” 另一个是“WPFUserCtrlLib”,它是类库。它的UserControl名为“WPFUserCtrl”,所以我有“WPFUser

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

请帮忙

编辑:

让我举例说明: 我有“WindowsFormApp1” 另一个是“WPFUserCtrlLib”,它是类库。它的UserControl名为“WPFUserCtrl”,所以我有“WPFUserCtrl.xaml”和“WPFUserCtrl.xaml.cs”文件。 在WindowsFormApp1中,我有一个名为“Form1”的主窗体。在表格1中,我使用元素主机使用“WPFUserCtrl”。 现在“WPFUserCtrl.xaml.cs”中有一些逻辑

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

所以这里我需要显示ErrorMsg。当显示此消息框时,它是非模态的,因为我可以访问“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);
            })
            );
}

现在我的MessageBox是model。

您能显示代码吗?您如何在WPF中显示模式窗口?可能与Hello derape重复,谢谢您的帮助。我已经访问了您发布的链接,但我认为我的问题有些不同。您好,谢谢@derape,这很有用,因为我在您发布的链接中遇到了相同的问题(另一个问题,然后是这个问题),因此解决了我的问题。:)