【发布时间】:2017-10-16 13:52:26
【问题描述】:
我为更新表单创建了一个新项目并为其使用了 WinForms(我的主要项目使用 WPF)。
所以我在 MainWindow 中创建了一些属性。其中之一是:
public Form Context
{
get { return this; }
}
当 MainWindow 是 WinForm 时,这很好。但它不是 WPF 窗口。 因此,由于错误,这不起作用。
The type MainWindow can not be converted in System.Windows.Forms.Form
有没有办法解决这个问题?
我尝试的是使用 WindowsFormsHost。所以我将 WindowsFormsHost 添加到我的 MainWindow:
<WindowsFormsHost>
<wf:Form x:Name="form" />
</WindowsFormsHost>
并编辑 MainWindow.xaml.cs 如下:
public Form Context
{
get { return form; }
}
但我遇到了 XamlParseException。
【问题讨论】:
-
您的 MainWindow 是
System.Windows.Window,但您的属性 'Context' 的类型是System.Windows.Forms.Form。它们是完全不同的类型,你想做什么? -
听起来您想在 WPF 应用程序中托管 Windows 窗体控件。尝试并按照本教程进行操作:docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/…
-
@mnistic 是的,很好。我需要什么 Context ?演练适用于 MaskedTextBox ^^
-
@Luranis 不确定您的意思?如果您需要更多帮助,则必须提供更多代码以帮助了解您在做什么。
-
我想在我的主窗口中打开一个 WinForms 对话框。这就是为什么表单上下文应该得到“这个”。但后来我得到了错误。所以我需要填写“这个”。我在另一个项目中按如下方式打开 WinForms:
if (new SharpUpdateAcceptForm(this.applicationInfo, update) .ShowDialog(this.applicationInfo.Context) == DialogResult.Yes) this.DownloadUpdate(update);