当前位置: 首页 > news >正文

dotnet 10 已知问题 WinForms 的 TargetFramework 与 System.Drawing.Common 不匹配将抛出找不到类型异常

此问题我已经在 WinForms 仓库反馈: https://github.com/dotnet/winforms/issues/14145

最简复现步骤如下:

先创建一个空的 .NET 项目,编辑 csproj 文件,替换为以下代码

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net9.0-windows</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable><UseWindowsForms>true</UseWindowsForms></PropertyGroup><ItemGroup><PackageReference Include="System.Drawing.Common" Version="10.0.1" /></ItemGroup>
</Project>

创建 Program.cs 文件,添加如下代码

System.Windows.Forms.SendKeys.SendWait("Hello, World!");

将项目里面的其余文件全删掉。然后构建运行项目

此时可见抛出如下错误

System.TypeInitializationException: The type initializer for 'System.Windows.Forms.SendKeys' threw an exception.---> System.TypeInitializationException: The type initializer for 'System.Windows.Forms.ScaleHelper' threw an exception.---> System.TypeLoadException: Could not load type 'System.Private.Windows.Core.OsVersion' from assembly 'System.Private.Windows.Core, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.at System.Windows.Forms.ScaleHelper.<InitializeStatics>g__GetPerMonitorAware|8_1()at System.Windows.Forms.ScaleHelper.InitializeStatics()--- End of inner exception stack trace ---at System.Windows.Forms.ScaleHelper.get_InitialSystemDpi()at System.Windows.Forms.Control..ctor(Boolean autoInstallSyncContext)at System.Windows.Forms.SendKeys.SKWindow..ctor()at System.Windows.Forms.SendKeys..cctor()--- End of inner exception stack trace ---at System.Windows.Forms.SendKeys.SendWait(String keys)at sendkey_error.MainWindow.Button_Click(Object sender, RoutedEventArgs e)

问题的原因是在 https://github.com/dotnet/winforms/pull/12839 里面修改了命名空间

这就导致了 OsVersion 类型从 System.Private.Windows.Core 命名空间改到 System.Private.Windows 命名空间,这里的不匹配就导致了类型找不到异常

本次问题属于情有可原,毕竟更新的是主版本号,主版本更新发生不兼容是合理的

此问题一开始是在 WPF 仓库报告的,但经过我调查,和 WPF 毫无关系,详细请看: https://github.com/dotnet/wpf/issues/11313

解决方法十分简单,保持 System.Drawing.Common 跟随主入口项目的 TargetFramework 版本,如修改 csproj 为如下代码

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net9.0-windows</TargetFramework><ImplicitUsings>enable</ImplicitUsings><UseWindowsForms>true</UseWindowsForms></PropertyGroup><ItemGroup><PackageReference Include="System.Drawing.Common" Version="9.0.11" /></ItemGroup>
</Project>

本文代码放在 github 和 gitee 上,可以使用如下命令行拉取代码。我整个代码仓库比较庞大,使用以下命令行可以进行部分拉取,拉取速度比较快

先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 1534db9e5f807d2c958cc8f7509f9f45229651e5

以上使用的是国内的 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码,将 gitee 源换成 github 源进行拉取代码。如果依然拉取不到代码,可以发邮件向我要代码

git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin 1534db9e5f807d2c958cc8f7509f9f45229651e5

获取代码之后,进入 WPFDemo/LaiwerelawkewaFeereajemle 文件夹,即可获取到源代码

更多技术博客,请参阅 博客导航

http://www.jsqmd.com/news/103266/

相关文章:

  • EmotiVoice技术深度解析:多情感TTS背后的秘密
  • 零样本声音克隆技术突破!EmotiVoice让TTS实现个性化音色复制
  • 算力网络中的数学理论
  • EmotiVoice语音合成模型的在线微调与反馈学习机制设想
  • EmotiVoice语音合成在语音贺卡小程序中的快速集成
  • 31、量子计算学习资源全解析
  • EmotiVoice能否用于电话机器人?合规性与技术可行性分析
  • EmotiVoice在博物馆导览系统中的沉浸式语音体验
  • 32、量子计算学习资源与常见误区揭秘
  • LobeChat PWA安装教程:添加到主屏,像原生App一样使用
  • 构建智能客服语音系统:EmotiVoice的情感化语音解决方案
  • EmotiVoice社区生态发展现状与贡献方式
  • 33、量子计算:打破迷思,探索未来
  • 34、量子计算:技术、商业与教育的全面洞察
  • EmotiVoice语音平滑度优化策略:减少断续感
  • 无需训练即可克隆声音?EmotiVoice零样本技术详解
  • 35、量子计算:从学术研究到实际应用
  • EmotiVoice语音情感强度量化指标研究
  • 用EmotiVoice为动画角色配音:全流程实践记录
  • EmotiVoice语音合成延迟优化:GPU加速实战配置指南
  • 商汤小浣熊3.0发布,百万级数据分析,一键生成高质量PPT
  • 深业云从人工智能产业投资基金设立,聚焦AI和具身智能相关产业
  • EmotiVoice在语音相册中为老照片配上回忆旁白
  • 22、量子计算、区块链在物流与运输领域的应用前景
  • 2、量子计算与区块链:技术碰撞与融合的探索
  • 3、量子计算与区块链技术入门
  • 4、量子计算:原理、算法与应用
  • 2025年下半年四川楼梯栏杆优质厂家推荐榜单 - 2025年品牌推荐榜
  • 2025年下半年重庆酒具厂家综合推荐:十大优质供应商解析 - 2025年品牌推荐榜
  • 2025年12月徐州箱变品牌选择指南与推荐榜单 - 2025年品牌推荐榜