dotnet resource_error ai_generated true

System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '42' and line position '10'.

ID: dotnet/wpf-resource-dictionary-key-not-found

Also available as: JSON · Markdown · 中文
90%Fix Rate
84%Confidence
1Evidence
2023-11-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
WPF on .NET Core 3.1 active
WPF on .NET 5.0 active
WPF on .NET 6.0 active
WPF on .NET 7.0 active
WPF on .NET 8.0 active
.NET Framework 4.7.2 active
.NET Framework 4.8 active

Root Cause

A StaticResource reference in WPF XAML points to a resource key that is not defined in any merged ResourceDictionary or is defined later in the same dictionary, causing a lookup failure at runtime.

generic

中文

WPF XAML 中的 StaticResource 引用指向了一个未在任何合并的 ResourceDictionary 中定义的资源键,或者在同一字典中定义得太晚,导致运行时查找失败。

Official Documentation

https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/resources-overview?view=netdesktop-8.0

Workarounds

  1. 95% success Define the missing resource key in the correct ResourceDictionary. For example, in App.xaml: <Application.Resources><ResourceDictionary><SolidColorBrush x:Key="MyBrush" Color="Blue"/></ResourceDictionary></Application.Resources>
    Define the missing resource key in the correct ResourceDictionary. For example, in App.xaml: <Application.Resources><ResourceDictionary><SolidColorBrush x:Key="MyBrush" Color="Blue"/></ResourceDictionary></Application.Resources>
  2. 90% success Ensure resource dictionaries are merged in the correct order. The resource must be defined before it is referenced. Move the resource definition above the element that uses it in the same file or merge dictionaries in the correct sequence.
    Ensure resource dictionaries are merged in the correct order. The resource must be defined before it is referenced. Move the resource definition above the element that uses it in the same file or merge dictionaries in the correct sequence.
  3. 70% success Use DynamicResource as a temporary workaround if the resource is defined but not available at load time (e.g., defined in a merged dictionary that is loaded later). However, prefer fixing the StaticResource lookup.
    Use DynamicResource as a temporary workaround if the resource is defined but not available at load time (e.g., defined in a merged dictionary that is loaded later). However, prefer fixing the StaticResource lookup.

中文步骤

  1. Define the missing resource key in the correct ResourceDictionary. For example, in App.xaml: <Application.Resources><ResourceDictionary><SolidColorBrush x:Key="MyBrush" Color="Blue"/></ResourceDictionary></Application.Resources>
  2. Ensure resource dictionaries are merged in the correct order. The resource must be defined before it is referenced. Move the resource definition above the element that uses it in the same file or merge dictionaries in the correct sequence.
  3. Use DynamicResource as a temporary workaround if the resource is defined but not available at load time (e.g., defined in a merged dictionary that is loaded later). However, prefer fixing the StaticResource lookup.

Dead Ends

Common approaches that don't work:

  1. Changing StaticResource to DynamicResource everywhere. 70% fail

    DynamicResource resolves at runtime but may cause performance issues and still fail if the resource is never defined; it also changes the behavior of resource updates.

  2. Adding the resource key with a null value in the same dictionary. 90% fail

    A null resource will cause a NullReferenceException when used; it does not solve the missing definition issue.

  3. Deleting the XAML file and recreating it from scratch. 100% fail

    This is an overreaction; the root cause is a missing resource definition, not file corruption.