dotnet/wpf · error · InvalidOperationException

SR.ResourceDictionaryDuplicateDeferredContent

Error message

SR.ResourceDictionaryDuplicateDeferredContent

What it means

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.

Solutions

  1. Use a new ResourceDictionary instance for each deferred-content load instead of reusing one
  2. Ensure DeferrableContent is assigned exactly once per dictionary
  3. If sharing resources, put the dictionary in a shared location (Application resources) rather than cloning the same instance
  4. Check custom XAML reader/writer code for duplicate calls into SetDeferrableContent

Example fix

// before
var dict = new ResourceDictionary();
mergedDicts.Add(dict);
mergedDicts2.Add(dict); // same instance gets deferred content twice
// after
mergedDicts.Add(new ResourceDictionary { Source = uri });
mergedDicts2.Add(new ResourceDictionary { Source = uri });
Defensive patterns

Strategy: validation

Validate before calling

if (dictDeferrableContentAlreadySet) throw new InvalidOperationException("Dictionary already has deferred content");

Try / catch

try { dict.DeferrableContent = content; } catch (InvalidOperationException ex) { /* create a fresh dictionary and retry */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs:1178

            _objectWriterFactory = deferrableContent.ObjectWriterFactory;
            _objectWriterSettings = deferrableContent.ObjectWriterParentSettings;
            _deferredLocationList = new List<KeyRecord>();
            _rootElement = deferrableContent.RootObject;

            IList<KeyRecord> keys = reader.ReadKeys();

            // If we already have the Source set then we can ignore
            // this deferable content section
            if (_source == null)
            {
                if (_reader == null)
                {
                    _reader = reader;
                    SetKeys(keys, deferrableContent.ServiceProvider);
                }
                else
                {
                    throw new InvalidOperationException(SR.ResourceDictionaryDuplicateDeferredContent);
                }
            }
            else if (keys.Count > 0)
            {
                throw new InvalidOperationException(SR.ResourceDictionaryDeferredContentFailure);
            }
        }

        private object GetKeyValue(KeyRecord key, IServiceProvider serviceProvider)
        {
            if (key.KeyString != null)
            {
                return key.KeyString;
            }
            else if (key.KeyType != null)
            {
                return key.KeyType;
            }

View on GitHub (pinned to 81131a70a4)