如何将DLL中的用户控件添加到.NET Core WinForms设计器工具箱?
.NET Core 6 WinForms 外部自定义控件无法添加到工具箱问题
问题场景
我正在使用Visual Studio 2022,尝试将原本可在.NET Framework 4.8 WinForms设计器中正常使用的组件和用户控件迁移到.NET Core 6项目的WinForms设计器,但遇到了无法解决的问题。
为简化问题,我做了如下测试:
- 自定义控件类库项目(.NET Core 6)
- 项目文件(
.csproj)内容:<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net6.0-windows</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup> </Project> - 仅包含的自定义控件类代码:
using System; using System.ComponentModel; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Runtime.Versioning; namespace MyCustomControls { [DisplayName(nameof(MyCustomControl))] [Description("My Custom Control.")] [DesignTimeVisible(true)] [ToolboxItem(true)] [DesignerCategory(nameof(UserControl))] [ToolboxBitmap(typeof(Control), "Control.bmp")] [ComVisible(true)] [SupportedOSPlatform("windows")] public class MyCustomControl : UserControl { public MyCustomControl() {} } }
- 项目文件(
- WinForms应用项目(.NET Core 6)
- 项目文件(
.csproj)内容:<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net6.0-windows</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup> </Project>
- 项目文件(
具体问题
将编译后的自定义控件.dll拖放到WinForms设计器的控件工具箱时,控件未被添加,且无任何警告或错误提示。已确认Visual Studio选项中Options -> General -> Windows Forms Designer下的AutoToolboxPopulate选项已启用,但未找到有效解决方案文档。
已尝试的排查操作
- 若在.NET Core WinForms项目内直接创建类文件并粘贴控件代码,编译后控件会自动添加至工具箱(仅限当前项目),因此怀疑是.NET Core对外部DLL控件导入的支持问题,但无法确认。
- 尝试重置工具箱、关闭Visual Studio并删除工具箱缓存文件(如
ToolboxItemDiscoveryCache.{long id here}),也参考了相关Stack Overflow解决方案,均未解决问题。
内容的提问来源于stack exchange,提问作者ElektroStudios
相关产品推荐
相关产品推荐

