本文记录 dotnet 10 的已知问题,将会导致 WPF 项目构建的时候给出错误

此问题开始是在 https://github.com/dotnet/wpf/issues/11246 被报告的

此问题已修复,详细请看 https://github.com/dotnet/dotnet/pull/3120

本文写于 2025年11月15日 此时为 .NET 10 首个正式版本发布,此问题为首个正式版本带来的。此问题在首个正式版本发布之后才修复,需要等待下个版本 SDK 发布才能解决此问题

详细错误信息如下

error CS0012: The type 'IImage' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.Windows.GdiPlus, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

如果等不及 SDK 更新,可使用以下补丁方法:

用户修复构建:

  <!-- Workaround for https://github.com/dotnet/wpf/issues/11246 -->
  <!-- Copy from https://github.com/dotnet/sdk/issues/51173#issuecomment-3392609295 -->
  <Target Name="_AddDrawingReference" AfterTargets="ResolveTargetingPackAssets">
    <ItemGroup>
      <Reference Condition="'%(Reference.FileName)' == 'PresentationCore'" 
                 Include="%(Reference.RootDir)%(Reference.Directory)System.Drawing.Common.dll;
                          %(Reference.RootDir)%(Reference.Directory)System.Private.Windows.GdiPlus.dll" />
    </ItemGroup>
  </Target>

用于自包含 self contained 应用:

  <!-- Copy from https://github.com/dotnet/windowsdesktop/issues/5405#issuecomment-3548429691 -->
  <Target Name="_AddDrawingRuntime" 
          AfterTargets="ResolveRuntimePackAssets"
          Condition="'$(SelfContained)' == 'true' AND '$(UseWPF)' == 'true' AND '$(UseWinforms)' != 'true'">
    <ItemGroup>
      <_additionalRuntimePackAsset Condition="'%(RuntimePackAsset.FileName)' == 'PresentationCore'" 
        Include="@(RuntimePackAsset->'%(RootDir)%(Directory)System.Drawing.Common.dll')" DestinationSubPath="System.Drawing.Common.dll" />
      <_additionalRuntimePackAsset Condition="'%(RuntimePackAsset.FileName)' == 'PresentationCore'" 
        Include="@(RuntimePackAsset->'%(RootDir)%(Directory)System.Private.Windows.GdiPlus.dll')" DestinationSubPath="System.Private.Windows.GdiPlus.dll" />
      <RuntimePackAsset Include="@(_additionalRuntimePackAsset)" />
      <ReferenceCopyLocalPaths Include="@(_additionalRuntimePackAsset)" />
    </ItemGroup>
  </Target>

等待下个 .NET 10 正式版本发布之后,以上补丁代码需要删掉

参考链接:

  • https://github.com/dotnet/wpf/issues/11261
  • https://github.com/dotnet/sdk/issues/51173
  • https://github.com/NuGet/Home/issues/11786
  • https://github.com/dotnet/windowsdesktop/issues/5405
  • https://github.com/dotnet/dotnet/pull/3402

本文会经常更新,请阅读原文: https://blog.lindexi.com/post/dotnet-10-%E5%B7%B2%E7%9F%A5%E9%97%AE%E9%A2%98-%E6%9E%84%E5%BB%BA-WPF-%E6%97%B6%E6%8F%90%E7%A4%BA-System.Private.Windows.GdiPlus-%E7%A8%8B%E5%BA%8F%E9%9B%86%E6%9C%AA%E6%89%BE%E5%88%B0%E9%94%99%E8%AF%AF.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者收藏我的博客导航

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

微软最具价值专家


无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号

欢迎进入 Eleven 老师组建的 .NET 社区

以上广告全是友情推广,无盈利