{"record":{"id":"f8882c22a2d3041c","repo":"dotnet/wpf","slug":"sr-markupextensionresourcenotfound","errorCode":null,"errorMessage":"SR.MarkupExtensionResourceNotFound","messagePattern":"SR\\.MarkupExtensionResourceNotFound","errorType":"exception","errorClass":"ResourceReferenceKeyNotFoundException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs","lineNumber":47,"sourceCode":"        {\n            FindResourceHelper helper = new FindResourceHelper(key);\n            return helper.TryCatchWhen();\n        }\n\n        private class FindResourceHelper\n        {\n            internal object TryCatchWhen()\n            {\n                Dispatcher.CurrentDispatcher.WrappedInvoke(new DispatcherOperationCallback(DoTryCatchWhen),\n                                                                                            null,\n                                                                                            1,\n                                                                                            new DispatcherOperationCallback(CatchHandler));\n                return _resource;\n            }\n\n            private object DoTryCatchWhen(object arg)\n            {\n                throw new ResourceReferenceKeyNotFoundException(SR.Format(SR.MarkupExtensionResourceNotFound, _name), _name);\n            }\n\n            private object CatchHandler(object arg)\n            {\n                _resource = DependencyProperty.UnsetValue;\n                return null;\n            }\n\n            public FindResourceHelper(object name)\n            {\n                _name = name;\n                _resource = null;\n            }\n\n            private object _name;\n            private object _resource;\n        }\n","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs#L29-L65","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use TryFindResource instead of FindResource and handle the null return.","Verify the exact key exists in the ResourceDictionary (keys are compared with the dictionary's comparer).","Ensure the dictionary defining the extension is merged before the lookup occurs.","Wrap FindResource in a try-catch for ResourceReferenceKeyNotFoundException when the resource is optional."],"exampleFix":"// before\nvar ext = (MarkupExtension)FindResource(\"MyExtensionKey\");\n// after\nvar ext = TryFindResource(\"MyExtensionKey\") as MarkupExtension;\nif (ext == null) { /* fall back or log */ }","handlingStrategy":"try-catch","validationCode":"bool exists = Application.Current.Resources.Contains(key);\n// plus per-dictionary: dict.Contains(key)","typeGuard":null,"tryCatchPattern":"try { resource = FindResource(key); }\ncatch (ResourceReferenceKeyNotFoundException) { resource = fallbackMarkupExtension; }","preventionTips":["Prefer TryFindResource for optional resources.","Centralize resource keys as constants to avoid typos.","Merge all needed dictionaries at startup, before lookups."],"tags":["wpf","resources","markup-extension","missing-key"],"backgroundTag":"resource-not-found","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}