dotnet/wpf · error · NotSupportedException

SR.NamesNotSupportedInsideResourceDictionary

Error message

SR.NamesNotSupportedInsideResourceDictionary

What it means

ResourceDictionary implements INameScope but explicitly does not support name registration; RegisterName always throws NotSupportedException (SR.NamesNotSupportedInsideResourceDictionary). Names (x:Name) are not allowed inside resource dictionaries.

Solutions

  1. Remove x:Name from elements inside ResourceDictionary entries; use x:Key instead for dictionary resources.
  2. If the element needs a name, move it out of the ResourceDictionary into the visual tree or a template with its own namescope.
  3. In programmatic code, check for ResourceDictionary before calling RegisterName on an INameScope.

Example fix

// before (XAML inside ResourceDictionary)
<ResourceDictionary><Button x:Name="okButton"/></ResourceDictionary>
// after
<ResourceDictionary><Button x:Key="okButton"/></ResourceDictionary>
Defensive patterns

Strategy: type-guard

Validate before calling

if (nameScope is ResourceDictionary) throw new NotSupportedException("ResourceDictionary does not support name registration; use x:Key for dictionary resources");

Type guard

bool SupportsNameRegistration(INameScope scope) => scope is not ResourceDictionary;

Try / catch

try { nameScope.RegisterName(name, element); } catch (NotSupportedException ex) when (ex.Message.Contains("names")) { /* fall back to x:Key semantics or skip */ }

Prevention

When it happens

Trigger: Calling INameScope.RegisterName on a ResourceDictionary, or XAML BAML/parse processing that encounters x:Name inside a ResourceDictionary and routes the registration to it.

Common situations: Declaring x:Name on an element inside a ResourceDictionary in XAML; framework code that programmatically registers names against any INameScope without checking the type.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

                ResourceDictionaryDiagnostics.AddResourceDictionaryForUri(uri, this);

                if (!IsInitializePending)
                {
                    // Fire Invalidations for the changes made by asigning a new Source
                    NotifyOwners(new ResourcesChangeInfo(null, this));
                }
            }
        }

        #region INameScope
        /// <summary>
        /// Registers the name - element combination
        /// </summary>
        /// <param name="name">name of the element</param>
        /// <param name="scopedElement">Element where name is defined</param>
        public void RegisterName(string name, object scopedElement)
        {
            throw new NotSupportedException(SR.NamesNotSupportedInsideResourceDictionary);
        }

        /// <summary>
        /// Unregisters the name - element combination
        /// </summary>
        /// <param name="name">Name of the element</param>
        public void UnregisterName(string name)
        {
            // Do Nothing as Names cannot be registered on ResourceDictionary
        }

        /// <summary>
        /// Find the element given name
        /// </summary>
        /// <param name="name">Name of the element</param>
        /// <returns>null always</returns>
        public object FindName(string name)
        {

View on GitHub (pinned to 81131a70a4)