dotnet/wpf · error · ArgumentException

SR.NameScopeNameNotFound

Error message

SR.NameScopeNameNotFound

What it means

UnregisterName throws ArgumentException (SR.NameScopeNameNotFound, including the name) when the name is not present in the name map — the fallback branch when _nameMap is null or the name has no entry. Unlike FindName, which returns null for missing names, unregistration of a nonexistent name is an error.

Solutions

  1. Verify the name exists (FindName != null) before unregistering
  2. Track registrations yourself and only unregister known names
  3. Make cleanup idempotent by catching/ignoring the not-found case or using a flag

Example fix

// before
nameScopeDictionary.UnregisterName("Button1"); // may not exist
// after
if (nameScopeDictionary.FindName("Button1") != null) nameScopeDictionary.UnregisterName("Button1");
Defensive patterns

Strategy: validation

Validate before calling

if (nameScopeDictionary.FindName(name) is not null) nameScopeDictionary.UnregisterName(name);

Type guard

static bool IsRegistered(NameScopeDictionary d, string name) => d.FindName(name) is not null;

Try / catch

try { nameScopeDictionary.UnregisterName(name); }
catch (ArgumentException) { /* name never registered */ }

Prevention

When it happens

Trigger: Calling UnregisterName for a name that was never registered, or that was already unregistered; double-unregister in cleanup paths; unregistering before the scope was populated.

Common situations: Idempotent cleanup code (e.g. on unload) calling UnregisterName twice; typos in name strings; scopes rebuilt between register and unregister calls.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs:98

            ArgumentNullException.ThrowIfNull(name);

            if (name.Length == 0)
                throw new ArgumentException(SR.NameScopeNameNotEmptyString);

            if (_underlyingNameScope is not null)
            {
                _underlyingNameScope.UnregisterName(name);
                _names.Remove(name);
            }
            else
            {
                if (_nameMap is not null && _nameMap[name] is not null)
                {
                    _nameMap.Remove(name);
                }
                else
                {
                    throw new ArgumentException(SR.Format(SR.NameScopeNameNotFound, name));
                }
            }
        }

        public object FindName(string name)
        {
            ArgumentNullException.ThrowIfNull(name);

            if (name.Length == 0)
                throw new ArgumentException(SR.NameScopeNameNotEmptyString);

            if (_underlyingNameScope is not null)
            {
                return _underlyingNameScope.FindName(name);
            }
            else
            {
                if (_nameMap is null)

View on GitHub (pinned to 81131a70a4)