无法解析 .Net Core 3.1 中的 System.Windows.Forms.dll

Unable to resolve System.Windows.Forms.dll in .Net Core 3.1

我有一个场景,我们在 .Net 核心 3.1 class 库中使用遗留的 .Net Framework dll。在内部 .Net Framework dll 使用 System.Windows.Forms.dll .Net 核心无法解析。

我在运行时收到以下错误消息

无法加载文件或程序集 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'。系统找不到指定的文件。

在理想世界中,将 System.Windows.Forms.dll 添加到 class 库是没有意义的,但是有什么方法可以在 .Net 核心 [=30] 中添加 System.Windows.Forms 及其依赖项=] 图书馆。

注:我试过手动添加System.Windows.Forms.dll,但没有成功。

我已经解决了,我们可以通过框架参考来实现。

<FrameworkReference Include="Microsoft.WindowsDesktop.App" /> 

详情请参考https://natemcmaster.com/blog/2019/01/09/netcore-primitives-3/

通过在项目 .csproj 文件中添加 <UseWindowsForms>True</UseWindowsForms> 使其在 .net core 3.1 中与我一起工作,变得像这样:

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWindowsForms>True</UseWindowsForms>
</PropertyGroup>