dotnet/wpf · error · InvalidOperationException
SR.Format(SR.NameScopeNotFound, name, "unregister")
Error message
SR.Format(SR.NameScopeNotFound, name, "unregister")
What it means
FrameworkContentElement.UnregisterName mirrors RegisterName: it needs the element's FindNameScope to return a NameScope from which the name is removed. When no namescope exists, InvalidOperationException with NameScopeNotFound("unregister") is thrown.
Solutions
- Guard with NameScope.GetNameScope(element) != null before unregistering.
- Unregister names while the element is still attached to its namescope-bearing tree.
- Skip the call when the whole tree/scope is being discarded, since the registrations die with it.
Example fix
// before
para.UnregisterName("p1"); // may throw when detached
// after
if (NameScope.GetNameScope(para)?.FindName("p1") != null)
para.UnregisterName("p1"); Defensive patterns
Strategy: type-guard
Validate before calling
if (NameScope.GetNameScope(element) == null) return; // nothing to unregister element.UnregisterName(name);
Type guard
static bool HasNameScope(FrameworkContentElement e) => NameScope.GetNameScope(e) != null;
Try / catch
try { element.UnregisterName(name); } catch (InvalidOperationException ex) when (ex.Message.Contains("unregister")) { /* element detached; skip */ } Prevention
- Unregister names while the element is still attached to its namescope-bearing tree.
- Check the namescope exists before unregistering; silently skip when detached.
- Centralize name registration/unregistration in one helper that manages scope lifecycle.
When it happens
Trigger: Calling UnregisterName(name) on a FrameworkContentElement that is detached from any XAML tree or was never given a namescope.
Common situations: Cleanup code unregistering names on elements already removed from the visual/logical tree (scope dropped); teardown paths running after the document/page unloaded.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- SR.Format(SR.CannotHavePropertyInTemplate…
- SR.Format(SR.NameNotFound, propertyValue.ChildName)
- SR.Format(SR.NameScopeDuplicateNamesNotAllowed, childName)
- SR.Format(SR.NameScopeNotFound, name, "register")
- SR.Format(SR.NameScopeNotFound, name, "register")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e5528ed57405c165.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkContentElement.cs:89
throw new InvalidOperationException(SR.Format(SR.NameScopeNotFound, name, "register"));
}
}
/// <summary>
/// Unregisters the name - element combination from the
/// NameScope that the current element belongs to.
/// </summary>
/// <param name="name">Name of the element</param>
public void UnregisterName(string name)
{
INameScope nameScope = FrameworkElement.FindScope(this);
if (nameScope != null)
{
nameScope.UnregisterName(name);
}
else
{
throw new InvalidOperationException(SR.Format(SR.NameScopeNotFound, name, "unregister"));
}
}
/// <summary>
/// Find the object with given name in the
/// NameScope that the current element belongs to.
/// </summary>
/// <param name="name">string name to index</param>
/// <returns>context if found, else null</returns>
public object FindName(string name)
{
DependencyObject scopeOwner;
return FindName(name, out scopeOwner);
}
// internal version of FindName that returns the scope owner
internal object FindName(string name, out DependencyObject scopeOwner)
{View on GitHub (pinned to 81131a70a4)