[英]NET Core 3.1 WinForms Designer incompatibility with WPF User Control
Over a year ago I began writing a .NET Framework 4.6.1 app using Windows Forms.一年多以前,我开始使用 Windows Forms 编写 .NET Framework 4.6.1 应用程序。 At the time I knew about WPF, yet was familiar with Windows Forms and it had most of the controls I needed.当时我知道 WPF,但熟悉 Windows Forms,它拥有我需要的大部分控件。 For the missing controls, I wrote two in Windows Forms and one in WPF.对于缺少的控件,我在 Windows Forms 中写了两个,在 WPF 中写了一个。 All of these coexisted fine, with the WPF control containerized within an element host.所有这些都很好地共存,将 WPF 控件容器化在一个元素主机中。
This week I began the migration process to .NET Core 3.1.本周我开始迁移到 .NET Core 3.1。 My tests with a copy of the project were positive, as well as initial results with the actual migration.我对项目副本的测试是积极的,实际迁移的初步结果也是如此。 After minor refactoring, the solution built and ran without issue.经过小的重构后,解决方案构建并运行没有问题。 Then the gremlin appeared after opening the main UI form in the WinForms Designer.然后在 WinForms Designer 中打开主 UI 表单后出现 gremlin。 Back in .NET Framework, all of my custom controls appeared inside the Designer's Toolbox, providing easy drag-and-drop onto the form.回到 .NET 框架中,我的所有自定义控件都出现在 Designer 的工具箱中,可以轻松拖放到表单上。 In .NET Core, only my WinForms controls appeared in the Toolbox, not my WPF control.在 .NET Core 中,只有我的 WinForms 控件出现在工具箱中,而不是我的 WPF 控件。 Because the Designer could not see that control, it stripped it from the Form's designer code, leaving an empty element host behind.因为设计器看不到该控件,所以它将它从窗体的设计器代码中剥离,留下一个空的元素宿主。
Here's the kicker.这是踢球者。 After reverting the Designer's changes, any direct manual edits to the Form's designer code is accepted, and building the project succeeds and runs fine.还原设计器的更改后,接受对表单设计器代码的任何直接手动编辑,并且构建项目成功并运行良好。 So for some reason the Designer does not like WPF controls in WinForms.所以出于某种原因,设计师不喜欢 WinForms 中的 WPF 控件。
Things I've tried:我尝试过的事情:
The point I'm at now is to keep manually editing the Form's designer code.我现在的重点是继续手动编辑表单的设计器代码。 Not ideal for large changes, and also not sustainable if/when this project is passed to another developer.不适合大的变化,如果/当这个项目被传递给另一个开发人员时也不可持续。 Any thoughts?有什么想法吗? Should I attempt porting the Windows Forms UI to WPF?我应该尝试将 Windows Forms UI 移植到 WPF 吗? Or is this simply a maturity issue with the still relatively new .NET Core Windows Forms Designer?或者这仅仅是相对较新的 .NET Core Windows Forms Designer 的成熟度问题?
Visual Studio version: Community 2019 16.9.3 Visual Studio 版本:社区 2019 16.9.3
Screenshot of IComponent error: enter image description here IComponent 错误的屏幕截图:在此处输入图像描述
I finally figured out a workaround;我终于想出了一个解决方法; the idea was sparked by the Microsoft Docs page for the ElementHost control: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.integration.elementhost?view=netcore-3.1这个想法是由 ElementHost 控件的 Microsoft Docs 页面引发的: https://docs.microsoft.com/en-us/dotnet/api/system.windows.ZAC68B62ABFD6A9FE26E38AC4236C8CE0=netcoreC.integration
In essence, move the WPF control hosting from the main UI into a WinForms control library.实质上,将 WPF 控件托管从主 UI 移动到 WinForms 控件库中。 Hence, the User control in this WinForms control library will be a wrapper for the WPF control.因此,此 WinForms 控件库中的用户控件将成为 WPF 控件的包装器。 Here are the steps I took in my test VS solution:以下是我在测试 VS 解决方案中采取的步骤:
using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
namespace WinFormsLibrary
{
public partial class WpfTest : UserControl
{
// ElementHost for the WPF control
private ElementHost host = new ElementHost();
// WPF control to be hosted
private WpfControlLibrary31.TestControl31 uc = new WpfControlLibrary31.TestControl31();
public WpfTest()
{
InitializeComponent();
}
private void WpfTest_Load(object sender, EventArgs e)
{
// set the docking style for the ElementHost
host.Dock = DockStyle.Fill;
// add the WPF control as a child of ElementHost
host.Child = uc;
// add the ElementHost as a control of the panel container
panelWpf.Controls.Add(host);
}
}
}
Thoughts :想法:
What are your thoughts?你觉得呢?你有没有什么想法? Thanks for the brainstorming help!感谢头脑风暴的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.