{"record":{"id":"728194ed0f3bc5a9","repo":"dotnet/wpf","slug":"sr-resourcedictionaryduplicatedeferredcontent","errorCode":null,"errorMessage":"SR.ResourceDictionaryDuplicateDeferredContent","messagePattern":"SR\\.ResourceDictionaryDuplicateDeferredContent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs","lineNumber":1178,"sourceCode":"            _objectWriterFactory = deferrableContent.ObjectWriterFactory;\n            _objectWriterSettings = deferrableContent.ObjectWriterParentSettings;\n            _deferredLocationList = new List<KeyRecord>();\n            _rootElement = deferrableContent.RootObject;\n\n            IList<KeyRecord> keys = reader.ReadKeys();\n\n            // If we already have the Source set then we can ignore\n            // this deferable content section\n            if (_source == null)\n            {\n                if (_reader == null)\n                {\n                    _reader = reader;\n                    SetKeys(keys, deferrableContent.ServiceProvider);\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.ResourceDictionaryDuplicateDeferredContent);\n                }\n            }\n            else if (keys.Count > 0)\n            {\n                throw new InvalidOperationException(SR.ResourceDictionaryDeferredContentFailure);\n            }\n        }\n\n        private object GetKeyValue(KeyRecord key, IServiceProvider serviceProvider)\n        {\n            if (key.KeyString != null)\n            {\n                return key.KeyString;\n            }\n            else if (key.KeyType != null)\n            {\n                return key.KeyType;\n            }","sourceCodeStart":1160,"sourceCodeEnd":1196,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs#L1160-L1196","documentation":"Thrown as InvalidOperationException from ResourceDictionary.SetDeferrableContent when deferred (BAML) content is being assigned to a dictionary that already has deferred content keys set. A ResourceDictionary can only receive its deferred content once; a second attempt indicates the same dictionary object is being reused or re-parsed from two sources.","triggerScenarios":"Calling DeferrableContent setter twice on the same ResourceDictionary, or a compiled BAML load path that associates deferred content with a dictionary whose _reader/keys were already initialized.","commonSituations":"Reusing a single ResourceDictionary instance as MergedDictionaries entry in multiple places; hand-crafted XAML parsing code that sets DeferrableContent repeatedly; unusual runtime XAML scenarios rather than ordinary app code.","solutions":["Use a new ResourceDictionary instance for each deferred-content load instead of reusing one","Ensure DeferrableContent is assigned exactly once per dictionary","If sharing resources, put the dictionary in a shared location (Application resources) rather than cloning the same instance","Check custom XAML reader/writer code for duplicate calls into SetDeferrableContent"],"exampleFix":"// before\nvar dict = new ResourceDictionary();\nmergedDicts.Add(dict);\nmergedDicts2.Add(dict); // same instance gets deferred content twice\n// after\nmergedDicts.Add(new ResourceDictionary { Source = uri });\nmergedDicts2.Add(new ResourceDictionary { Source = uri });","handlingStrategy":"validation","validationCode":"if (dictDeferrableContentAlreadySet) throw new InvalidOperationException(\"Dictionary already has deferred content\");","typeGuard":null,"tryCatchPattern":"try { dict.DeferrableContent = content; } catch (InvalidOperationException ex) { /* create a fresh dictionary and retry */ }","preventionTips":["Never share a ResourceDictionary instance across multiple merge points","Assign DeferrableContent exactly once, right after construction"],"tags":["wpf","resourcedictionary","xaml","deferred-content"],"backgroundTag":"invalid-state-transition","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"}