是否可以通过容器/包装器在 WPF 表单中托管 WinForm 表单?
有没有办法在 WPF 表单中的某种容器或包装类型控件中托管/显示完整的 WinForms 表单(而不仅仅是单个控件)?我正在寻找与 html 中的 php 或 iframe 的虚拟包含概念类似的东西。可能通过将其编译成 OCX 或 DLL 来实现。
Is there a way to host/display a full WinForms form (not just a single control) within some sort of container or wrapper type control within a WPF form? I’m looking for something similar in concept to a virtual include from php or iframe in html. Possibly by compiling it into an OCX or DLL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用以下代码来完成此操作:
在您的 XAML 中:
在您的代码隐藏中:
you can do it with the following code:
In your XAML:
In your code-behind:
据我所知,您无法在 WPF 中托管实际的 WinForms 表单。
然而,一个好的替代方法是在 WinForms 中创建一个普通的 UserControl(作为 Windows 窗体控件库项目),其中包含所有必需的功能和子控件。
然后在您的 WPF 项目中,引用 WindowsFormsIntegration.dll(应位于“添加引用”的 .NET 选项卡中)。
然后还引用包含 WinForms UserControl 的程序集,最后将 WindowsFormsHost 容器添加到 XAML。
有关详细信息,请参阅 Sacha Barber 撰写的实用教程。
To my knowledge you can't host an actual WinForms Form in WPF.
However a good alternative is to create a normal UserControl in WinForms (as a Windows Forms Control Library project) that contains all required functionality and child controls.
Then in your WPF project, reference the WindowsFormsIntegration.dll (should be in the .NET tab of Add References).
Then also reference the assembly containing the WinForms UserControl and finally add a WindowsFormsHost container to the XAML.
For more information see this useful tutorial by Sacha Barber.
CodeProject 上有一篇文章,展示了如何将 exe 托管为控件在 WinForms 上(这是一个使用选项卡页的应用程序,将 Winform.exe 拖到其上,它会创建一个包含该窗口的选项卡页),源自这篇文章 此处。我知道您特别要求 WPF,但这也许会引导您走向正确的方向?
希望这有帮助,
此致,
汤姆.
There is an article on CodeProject that shows how to host a exe as a control on the WinForms (It's an application that uses Tab pages, drag a Winform.exe onto it, it creates a tabpage with that window in it), derived from this article here. I know you asked for specifically WPF, but perhaps that might lead you in the right direction?
Hope this helps,
Best regards,
Tom.