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

如何在UWP应用的App.xaml中配置全局ThemeDictionaries?

问题原因与解决方法

错误XDG0046的根源是你多嵌套了一层<ResourceDictionary>标签。

UWP的Application.Resources本身就是一个ResourceDictionary容器,不需要在外层额外包裹<ResourceDictionary>标签。你的代码里外层的这个无键ResourceDictionary会被视为字典条目,但缺少必填的x:Key,因此触发错误。

修正后的全局主题资源代码(放在App.xaml中)

<Application
    x:Class="YourAppName.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:YourAppName">
    <Application.Resources>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Light">
                <SolidColorBrush x:Key="BackgroundBrush" Color="White" />
                <SolidColorBrush x:Key="ForegroundBrush" Color="Black" />
            </ResourceDictionary>
            <ResourceDictionary x:Key="Dark">
                <SolidColorBrush x:Key="BackgroundBrush" Color="Black" />
                <SolidColorBrush x:Key="ForegroundBrush" Color="White" />
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </Application.Resources>
</Application>

额外说明

  • 如果需要合并其他资源字典,可以在Application.Resources内使用<ResourceDictionary.MergedDictionaries>,此时每个合并的ResourceDictionary需要指定x:Key。
  • 主题键Light和Dark是UWP系统预定义的有效值,会自动跟随系统主题切换生效。

内容的提问来源于stack exchange,提问作者Vladyslav Yakubovskyi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 08:12:32