dotnet/wpf · error · ResourceReferenceKeyNotFoundException

SR.MarkupExtensionResourceNotFound

Error message

SR.MarkupExtensionResourceNotFound

What it means

When a StaticResource/DynamicResource lookup targets a markup extension stored in a ResourceDictionary and the key is not found, this deferred-resource helper throws ResourceReferenceKeyNotFoundException with MarkupExtensionResourceNotFound, naming the missing resource key. It surfaces during the dispatcher callback that finalizes the failed resource lookup.

Solutions

  1. Use TryFindResource instead of FindResource and handle the null return.
  2. Verify the exact key exists in the ResourceDictionary (keys are compared with the dictionary's comparer).
  3. Ensure the dictionary defining the extension is merged before the lookup occurs.
  4. Wrap FindResource in a try-catch for ResourceReferenceKeyNotFoundException when the resource is optional.

Example fix

// before
var ext = (MarkupExtension)FindResource("MyExtensionKey");
// after
var ext = TryFindResource("MyExtensionKey") as MarkupExtension;
if (ext == null) { /* fall back or log */ }
Defensive patterns

Strategy: try-catch

Validate before calling

bool exists = Application.Current.Resources.Contains(key);
// plus per-dictionary: dict.Contains(key)

Try / catch

try { resource = FindResource(key); }
catch (ResourceReferenceKeyNotFoundException) { resource = fallbackMarkupExtension; }

Prevention

When it happens

Trigger: Calling FindResource (or StaticResource resolution) with a key whose deferred resource is a markup extension, where the key does not exist in any reachable ResourceDictionary — e.g. FindResource("MyExtensionKey") with no such entry.

Common situations: Typos in resource keys, referencing a dictionary merged at runtime after the lookup, case-sensitivity differences in keys, and markup extensions removed from App.xaml resources during refactoring.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f8882c22a2d3041c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs:47

        {
            FindResourceHelper helper = new FindResourceHelper(key);
            return helper.TryCatchWhen();
        }

        private class FindResourceHelper
        {
            internal object TryCatchWhen()
            {
                Dispatcher.CurrentDispatcher.WrappedInvoke(new DispatcherOperationCallback(DoTryCatchWhen),
                                                                                            null,
                                                                                            1,
                                                                                            new DispatcherOperationCallback(CatchHandler));
                return _resource;
            }

            private object DoTryCatchWhen(object arg)
            {
                throw new ResourceReferenceKeyNotFoundException(SR.Format(SR.MarkupExtensionResourceNotFound, _name), _name);
            }

            private object CatchHandler(object arg)
            {
                _resource = DependencyProperty.UnsetValue;
                return null;
            }

            public FindResourceHelper(object name)
            {
                _name = name;
                _resource = null;
            }

            private object _name;
            private object _resource;
        }

View on GitHub (pinned to 81131a70a4)