You need to enable JavaScript to run this app. WPF集成XAML Islands时如何正确配置System.Runtime.WindowsRuntime.dll
优惠活动
大模型
产品
解决方案
定价
更多

WPF集成XAML Islands时如何正确配置System.Runtime.WindowsRuntime.dll

WPF应用集成XAML Islands时的程序集引用冲突问题

开发背景

正在开发一款WPF应用,计划集成XAML Islands以调用Windows 10原生控件。参照微软官方XAML Islands深度教程的步骤,通过NuGet包管理器添加Microsoft.Toolkit.Wpf.UI.XamlHost包,安装过程中自动引入了全套依赖项,包括Microsoft.Windows.SDK.Contracts (10.0.18362.2005),该包的依赖规则要求System.Runtime.WindowsRuntime >= 4.6.0版本。

首次出现的编译错误

运行项目时直接触发编译报错:

Multiple assemblies with equivalent identity have been imported: 
'C:\Users\*[项目本地路径]*\packages\System.Runtime.WindowsRuntime.4.6.0\ref\netstandard2.0\System.Runtime.WindowsRuntime.dll'
and 
'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll'.
Remove one of the duplicate references.

错误原因定位很明确:XamlHost包安装时自动修改了app.config和packages.config文件,但项目原有的MyProject.csproj文件中,本来就预置了System.Runtime.WindowsRuntime.dll的引用,引用配置如下:

<Reference Include="System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll</HintPath>
</Reference>

尝试过的修复操作及遇到的新问题

  • 第一次尝试:选择保留XamlHost自带的依赖,移除上述csproj中预置的旧引用。编译流程顺利通过,但启动应用时弹出报错:This application has failed to start because the application configuration is incorrect.
  • 第二次尝试:反过来移除XamlHost安装时新增的引用配置,具体操作包括:
    • 从app.config中删除对应的绑定重定向配置:
      <dependentAssembly>
          <assemblyIdentity name="System.Runtime.WindowsRuntime" publicKeyToken="b77a5c561934e089" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-4.6.0.0" newVersion="4.6.0.0" />
      </dependentAssembly>
      
    • 从packages.config中删除对应的NuGet包记录:
      <package id="System.Runtime.WindowsRuntime" version="4.6.0" targetFramework="net471" />
      
    操作完成后启动应用,仍然弹出完全相同的“应用配置不正确”的报错。

当前开发环境已经完成全量更新,包括最新版Visual Studio 2019社区版、最新Windows SDK、最新.NET Framework,问题仍未解决。

核心待解疑问

  • 为什么MyProject.csproj中除System.Runtime.WindowsRuntime之外的所有引用都指向本地packages目录,只有这个dll的引用指向Program Files路径?尝试过将引用版本调整为4.6.0、把引用路径替换为packages目录下的对应dll,所有调整都没有生效;如果完全移除这个引用,项目甚至无法正常编译。
  • Program Files路径下的System.Runtime.WindowsRuntime.dll所在的.NETCore\v4.5\目录到底是什么?当时最新的.NET Core版本已经迭代到3.1,该目录下的System.Runtime.WindowsRuntime.dll却是4.0.0版本,为什么会存在这样的路径和对应程序集?
  • 当前项目的配置到底存在什么问题?为什么两种删除重复引用的方案,都会触发应用启动时的配置错误报错?

内容的提问来源于stack exchange,提问作者Attila Szász

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.19 01:45:07