【问题标题】:C# UWP Build failed / Windows.Foundation.FoundationContract missingC# UWP 构建失败/缺少 Windows.Foundation.FoundationContract
【发布时间】:2018-11-29 12:38:02
【问题描述】:

我将一个 UWP 应用更新为较新的 SDK,并将目标版本设置为 16299,将最低版本设置为 15063。

然后我使用 Windows.Foundation.UniversalApiContract 命名空间来使用新 SDK 的功能(如果可用)。示例:

xmlns:contract5NotPresent="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractNotPresent(Windows.Foundation.UniversalApiContract,5)"
xmlns:contract5Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,5)"

<contract5Present:NavigationView x:Name="NavView" ...

构建失败,没有显示错误消息。但是当我将构建日志设置为详细时,它似乎找不到程序集:

Could not resolve this reference. Could not locate the assembly "Windows.Foundation.FoundationContract, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

文件夹和文件存在于 C:\Program Files (x86)\Windows Kits\10\References**10.0.16299.0**\Windows.Foundation.FoundationContract\3.0.0.0 和 C:\Program Files (x86)\Windows Kits\10\References**10.0.15063.0**\Windows.Foundation.FoundationContract\3.0.0.0

更新 1: 看来这与我的资源字典和丙烯酸刷子有关。如果我将资源字典复制到空白应用程序,则会发生相同的错误。如果我从资源字典中删除合同内容和所有丙烯酸刷子,则构建成功。那么我在这里错过了什么?

更新 2: 我终于找到了问题所在(也许你可以重现,只需从一个空白应用程序开始(目标:Fall Creators,最低版本:Creators Update),创建一个 ResourceDictionary 并将其复制到其中):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="using:App6"
                xmlns:contract5NotPresent="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractNotPresent(Windows.Foundation.UniversalApiContract,5)"
                xmlns:contract5Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,5)"
                >

<Thickness x:Key="HeaderMargin">20,41,0,0</Thickness>

<ResourceDictionary.ThemeDictionaries>
    <ResourceDictionary x:Key="Light">
        <Color x:Key="BrandColor">#D1D1D1</Color>
        <contract5Present:AcrylicBrush x:Key="OptionsPaneBackground" BackgroundSource="Backdrop" TintColor="{StaticResource BrandColor}" TintOpacity="0.2" FallbackColor="#D1D1D1" />
    </ResourceDictionary>

    <ResourceDictionary x:Key="Dark">
        <Color x:Key="BrandColor">#3F3F46</Color>
        <contract5Present:AcrylicBrush x:Key="OptionsPaneBackground" BackgroundSource="Backdrop" TintColor="{StaticResource BrandColor}" TintOpacity="0.6" FallbackColor="#3F3F46" />
    </ResourceDictionary>

</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>

如果我从字典中删除第一行(厚度),则构建成功。我的系统在 2018 年 4 月更新上运行,但我在另一台安装了 Fall Creators Update 的机器上遇到了同样的问题。

解决方案:我已将 ThemeResources 和 Styles 拆分为两个单独的 ResourceDictionaries,现在在我的主项目中构建成功。但我不明白为什么会发生这种情况以及为什么没有出现确切的错误消息。我花了大约 8 个小时(我的项目有超过 70000 行代码)来发现问题,对此我有点生气:)

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

    您的 16299 和 15063 SDK 文件似乎有问题,您可以尝试打开文件资源管理器并转到相应的文件位置查看 SDK 是否存在。

    路径应该是:C:\Program Files (x86)\Windows Kits\10\References\10.0.15063.0(10.0.16299.0)\Windows.Foundation.FoundationContract\3.0.0.0\

    否则,您可以尝试从Windows SDK and emulator archive重新下载并安装SDK版本。

    如果仍然无法正常工作,请尝试将 Microsoft.NETCore.UniversalWindowsPlatform Nuget 更新到最新版本,然后重新启动您的 VS,再次清理并重新构建您的应用。顺便说一句,还请提供您的设备操作系统版本和操作系统版本,以帮助我调查此问题。

    ---更新---

    另一种解决方法是您可以在&lt;ResourceDictionary.ThemeDictionaries&gt;&lt;/ResourceDictionary.ThemeDictionaries&gt; 标记对之后 声明ResourceDictionary 的内容。这将防止触发崩溃。

    【讨论】:

    • 更新了我最初的帖子。
    • AcrylicBrush 是在 10.0.16299.0 版本中引入的,你现在使用它吗?你能提供一个简单的可重复的样品吗?您的设备操作系统版本是多少?
    • 初始帖子的新更新。
    • @PeterFrommert 很抱歉给您带来不便,这个问题是我们系统中的一个已知问题。目前,解决方法就像您将 ThemeResources 和 Styles 拆分为两个单独的 ResourceDictionaries。
    • 此外,还有另一个变通方法是在 标记对之后声明 ResourceDictionary 的内容。
    猜你喜欢
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 2016-03-25
    • 2019-12-14
    • 2017-11-08
    相关资源
    最近更新 更多