{"record":{"id":"e5528ed57405c165","repo":"dotnet/wpf","slug":"sr-format-sr-namescopenotfound-name-unregister","errorCode":null,"errorMessage":"SR.Format(SR.NameScopeNotFound, name, \"unregister\")","messagePattern":"SR\\.Format\\(SR\\.NameScopeNotFound, name, \"unregister\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkContentElement.cs","lineNumber":89,"sourceCode":"                throw new InvalidOperationException(SR.Format(SR.NameScopeNotFound, name, \"register\"));\n            }\n        }\n\n        /// <summary>\n        /// Unregisters the name - element combination from the\n        /// NameScope that the current element belongs to.\n        /// </summary>\n        /// <param name=\"name\">Name of the element</param>\n        public void UnregisterName(string name)\n        {\n            INameScope nameScope = FrameworkElement.FindScope(this);\n            if (nameScope != null)\n            {\n                nameScope.UnregisterName(name);\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.Format(SR.NameScopeNotFound, name, \"unregister\"));\n            }\n        }\n\n        /// <summary>\n        /// Find the object with given name in the\n        /// NameScope that the current element belongs to.\n        /// </summary>\n        /// <param name=\"name\">string name to index</param>\n        /// <returns>context if found, else null</returns>\n        public object FindName(string name)\n        {\n            DependencyObject scopeOwner;\n            return FindName(name, out scopeOwner);\n        }\n\n        // internal version of FindName that returns the scope owner\n        internal object FindName(string name, out DependencyObject scopeOwner)\n        {","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkContentElement.cs#L71-L107","documentation":"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.","triggerScenarios":"Calling UnregisterName(name) on a FrameworkContentElement that is detached from any XAML tree or was never given a namescope.","commonSituations":"Cleanup code unregistering names on elements already removed from the visual/logical tree (scope dropped); teardown paths running after the document/page unloaded.","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."],"exampleFix":"// before\npara.UnregisterName(\"p1\"); // may throw when detached\n// after\nif (NameScope.GetNameScope(para)?.FindName(\"p1\") != null)\n    para.UnregisterName(\"p1\");","handlingStrategy":"type-guard","validationCode":"if (NameScope.GetNameScope(element) == null) return; // nothing to unregister\nelement.UnregisterName(name);","typeGuard":"static bool HasNameScope(FrameworkContentElement e) => NameScope.GetNameScope(e) != null;","tryCatchPattern":"try { element.UnregisterName(name); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"unregister\")) { /* element detached; skip */ }","preventionTips":["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."],"tags":["wpf","namescope","xaml","cleanup"],"backgroundTag":"resource-not-found","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}