You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一解决方案中项目的WinRT.Runtime版本不兼容问题(WinUI3、.NET 8)

解决WinRT.Runtime版本冲突导致的编译失败问题

问题背景

解决方案无语法错误,更新Visual Studio后无法完成编译。已尝试清理重建解决方案、更新NuGet包,问题仍存在。所有项目基于.NET 8,仅View项目报错,其他同版本项目可正常编译。

错误信息

程序集“CustomControls”(标识:CustomControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)引用了WinRT.Runtime 2.2.0.0版本,而当前View项目引用的WinRT.Runtime版本为2.1.0.0,版本不匹配导致编译失败。

相关项目文件

CustomControls.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>CustomControls</RootNamespace>
    <RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &gt;= 8">win-x86;win-x64;win-arm64</RuntimeIdentifiers>
    <RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &lt; 8">win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <UseWinUI>true</UseWinUI>
    <PublishTrimmed>False</PublishTrimmed>
    <TrimMode>partial</TrimMode>
  </PropertyGroup>

  <ItemGroup>
      <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.250108002" />
      <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Converters\Converters.csproj" />
  </ItemGroup>
</Project>

View.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>View</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;ARM64</Platforms>
    <RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &gt;= 8">win-x86;win-x64;win-arm64</RuntimeIdentifiers>
    <RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &lt; 8">win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <PublishProfile>win-$(Platform).pubxml</PublishProfile>
    <UseWinUI>true</UseWinUI>
    <EnableMsixTooling>true</EnableMsixTooling>
    <WindowsSdkPackageVersion>10.0.19041.38</WindowsSdkPackageVersion>
    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
    <PublishTrimmed>False</PublishTrimmed>
    <TrimMode>partial</TrimMode>
    <DefaultLanguage>pt-BR</DefaultLanguage>
    <GenerateAppInstallerFile>False</GenerateAppInstallerFile>
    <AppxPackageSigningEnabled>False</AppxPackageSigningEnabled>
    <AppxPackageSigningTimestampDigestAlgorithm>SHA256</AppxPackageSigningTimestampDigestAlgorithm>
    <AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision>
    <AppxSymbolPackageEnabled>False</AppxSymbolPackageEnabled>
    <GenerateTestArtifacts>True</GenerateTestArtifacts>
    <AppxBundle>Never</AppxBundle>
    <HoursBetweenUpdateChecks>0</HoursBetweenUpdateChecks>
  </PropertyGroup>
  <ItemGroup>
    ...
  </ItemGroup>

  <ItemGroup>
    ....
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
    <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.250108002" />
    <PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
    <PackageReference Include="System.Private.Uri" Version="4.3.2" />
    <PackageReference Include="System.Text.Json" Version="9.0.1" />
    <Manifest Include="$(ApplicationManifest)" />
  </ItemGroup>

  <!--
    Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
    Tools extension to be activated for this project even if the Windows App SDK Nuget
    package has not yet been restored.
  -->
  <ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
    <ProjectCapability Include="Msix" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Converters\Converters.csproj" />
    <ProjectReference Include="..\CustomControls\CustomControls.csproj" />
    <ProjectReference Include="..\Dados\Data.csproj" />
    <ProjectReference Include="..\UserAuthentication\UserAuthentication.csproj" />
    <ProjectReference Include="..\WinUI.TableView\WinUI.TableView.csproj" />
  </ItemGroup>
  [...]
    <!--
    Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
    Explorer "Package and Publish" context menu entry to be enabled for this project even if
    the Windows App SDK Nuget package has not yet been restored.
  -->
  <PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
    <HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
  </PropertyGroup>
</Project>

解决步骤

1. 显式统一WinRT.Runtime版本

在View项目的.csproj文件中,添加WinRT.Runtime的显式包引用,指定与CustomControls依赖一致的版本:

<ItemGroup>
  <!-- 其他包引用 -->
  <PackageReference Include="WinRT.Runtime" Version="2.2.0" />
</ItemGroup>

2. 添加版本绑定重定向(若第一步无效)

如果显式引用仍无法解决,在View项目中添加绑定重定向规则,强制使用新版本:

方式1:使用runtimeconfig.template.json(.NET 8推荐)

在项目根目录创建runtimeconfig.template.json,添加内容:

{
  "runtimeOptions": {
    "assemblyBinding": {
      "dependentAssembly": [
        {
          "assemblyIdentity": {
            "name": "WinRT.Runtime",
            "publicKeyToken": "99ea127f02d97709",
            "culture": "neutral"
          },
          "bindingRedirect": {
            "oldVersion": "0.0.0.0-2.2.0.0",
            "newVersion": "2.2.0.0"
          }
        }
      ]
    }
  }
}

方式2:使用app.config

在项目中添加app.config文件,添加内容:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WinRT.Runtime" publicKeyToken="99ea127f02d97709" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

3. 清理缓存并重建

  • 清理解决方案:点击「生成」→「清理解决方案」
  • 删除所有项目的bin和obj文件夹
  • 清除NuGet缓存:「工具」→「NuGet包管理器」→「包管理器设置」→「清除所有NuGet缓存」
  • 还原NuGet包:右键解决方案→「还原NuGet包」
  • 重新生成解决方案

4. 检查依赖版本一致性

确认所有项目的Microsoft.WindowsAppSDK版本完全一致,必要时更新到最新稳定版,避免依赖链中的版本冲突。


内容的提问来源于stack exchange,提问作者Arthur Buzas Baccan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 13:42:02