dotnet/wpf · error · InvalidOperationException

SR.ResourceDictionaryDeferredContentFailure

Error message

SR.ResourceDictionaryDeferredContentFailure

What it means

Thrown as InvalidOperationException from ResourceDictionary.SetDeferrableContent when the dictionary already contains regular (non-deferred) keys but is then given deferred content. A dictionary must be populated either entirely through deferred BAML content or through direct key insertion — mixing both is not supported.

Solutions

  1. Set DeferrableContent before adding any keys to the dictionary
  2. Use a fresh empty ResourceDictionary for deferred content
  3. Never mix programmatic key insertion with deferred BAML content in one dictionary

Example fix

// before
dict["myKey"] = myValue;
dict.DeferrableContent = content; // throws
// after
dict.DeferrableContent = content; // set first
// access keys only through the deferred mechanism
Defensive patterns

Strategy: validation

Validate before calling

if (dict.Keys.Count > 0) throw new InvalidOperationException("Deferred content requires an empty dictionary");

Try / catch

try { dict.DeferrableContent = content; } catch (InvalidOperationException) { dict = new ResourceDictionary(); dict.DeferrableContent = content; }

Prevention

When it happens

Trigger: Adding keys (e.g. dict["key"]=value or MergedDictionaries) to a ResourceDictionary and afterwards setting DeferrableContent on it.

Common situations: Custom XAML/BAML loading code that pre-populates a dictionary then attaches deferred content; malformed generated dictionaries.

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/32d9d01b277020a2. Report an issue: GitHub.

Appendix: source

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

            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;
            }
            else
            {
                System.Xaml.XamlReader reader = key.KeyNodeList.GetReader();
                object value = EvaluateMarkupExtensionNodeList(reader, serviceProvider);
                return value;

View on GitHub (pinned to 81131a70a4)