如何在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
相关产品推荐
相关产品推荐

