VS2022 WPF .NET中如何修复XDG0062错误?
解决Error XDG0062: 'StaticResourceExtension' is not valid for Setter.Value的问题
问题根源
你在ControlTemplate.xaml里用StaticResource引用了EditorColors.xaml中的画笔资源,但两个资源字典之间没有建立合并引用关系,导致XAML解析器无法找到这些资源,进而抛出该错误(错误信息表述存在误导,本质是资源未被正确解析)。
修复方法
- 在
ControlTemplate.xaml的ResourceDictionary节点内部添加对EditorColors.xaml的合并引用:
<ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="EditorColors.xaml"/> </ResourceDictionary.MergedDictionaries>
若两个文件不在同一目录,需调整Source路径(例如使用包路径/Pico_Editor.Dictionaries;component/EditorColors.xaml)。
- 确保主窗口的资源字典正确引入这两个文件,或直接引用包含它们的合并字典,保证主窗口使用
PicoWindowStyle时能找到所有依赖资源。
修改后的ControlTemplate.xaml示例
<ResourceDictionary x:Class="Pico_Editor.Dictionaries.ControlTemplates" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Pico_Editor.Dictionaries"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="EditorColors.xaml"/> </ResourceDictionary.MergedDictionaries> <Style TargetType="{x:Type TextBox}" x:Key="TextBoxStyle"> <!-- TODO: define the look of textbox here --> </Style> <Style TargetType="{x:Type TextBox}" x:Key="{x:Type TextBox}" BasedOn="{StaticResource TextBoxStyle}"> <EventSetter Event="KeyDown" Handler="OnTextBox_KeyDown"/> </Style> <Style x:Key="PicoWindowStyle" TargetType="{x:Type Window}"> <Setter Property="BorderBrush" Value="{StaticResource Editor.Selected.BackgroundBrush}"/> <Setter Property="Background" Value="{StaticResource Editor.Window.GrayBrush1}"/> </Style> </ResourceDictionary>
备选方案
若合并后仍存在加载顺序问题,可临时替换为DynamicResource(性能略逊,但能延迟资源查找,适配加载顺序场景):
<Setter Property="BorderBrush" Value="{DynamicResource Editor.Selected.BackgroundBrush}"/> <Setter Property="Background" Value="{DynamicResource Editor.Window.GrayBrush1}"/>
内容的提问来源于stack exchange,提问作者Daniel McLarty
相关产品推荐
相关产品推荐

