{"record":{"id":"d62a9c645a3966de","repo":"dotnet/maui","slug":"this-element-is-not-in-a-namescope","errorCode":null,"errorMessage":"this element is not in a namescope","messagePattern":"this element is not in a namescope","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Element/Element.cs","lineNumber":572,"sourceCode":"\t\t/// </para>\n\t\t/// <list type=\"bullet\">\n\t\t/// <item><description>Elements with x:Name attributes defined in XAML within the same namescope</description></item>\n\t\t/// <item><description>Elements manually registered using <see cref=\"INameScope.RegisterName(string, object)\"/></description></item>\n\t\t/// <item><description>Child elements and their descendants within the same namescope boundary</description></item>\n\t\t/// </list>\n\t\t/// <para>\n\t\t/// Elements in different namescopes (such as different pages or data templates) are not accessible\n\t\t/// from each other through this method.\n\t\t/// </para>\n\t\t/// </remarks>\n\t\tpublic object FindByName(string name)\n\t\t{\n\t\t\tif (!RuntimeFeature.AreNamescopesSupported)\n\t\t\t\tthrow new NotSupportedException(\"Namescopes are not supported. Please enable the feature switch 'Microsoft.Maui.RuntimeFeature.AreNamescopesSupported' to keep using namescopes.\");\n\n\t\t\tvar namescope = GetNameScope() ?? transientNamescope;\n\t\t\tif (namescope == null)\n\t\t\t\tthrow new InvalidOperationException(\"this element is not in a namescope\");\n\t\t\treturn namescope.FindByName(name);\n\t\t}\n\n\t\t/// <inheritdoc/>\n\t\tvoid INameScope.RegisterName(string name, object scopedElement)\n\t\t{\n\t\t\tif (!RuntimeFeature.AreNamescopesSupported)\n\t\t\t\tthrow new NotSupportedException(\"Namescopes are not supported. Please enable the feature switch 'Microsoft.Maui.RuntimeFeature.AreNamescopesSupported' to keep using namescopes.\");\n\n\t\t\tvar namescope = GetNameScope() ?? throw new InvalidOperationException(\"this element is not in a namescope\");\n\t\t\tnamescope.RegisterName(name, scopedElement);\n\t\t}\n\n\t\t/// <inheritdoc/>\n\t\tvoid INameScope.UnregisterName(string name)\n\t\t{\n\t\t\tif (!RuntimeFeature.AreNamescopesSupported)\n\t\t\t\tthrow new NotSupportedException(\"Namescopes are not supported. Please enable the feature switch 'Microsoft.Maui.RuntimeFeature.AreNamescopesSupported' to keep using namescopes.\");","sourceCodeStart":554,"sourceCodeEnd":590,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Element/Element.cs#L554-L590","documentation":"Thrown by Element.FindByName when namescopes are enabled but no INameScope can be resolved for the element. FindNameScope traverses up the RealParent chain looking for a registered namescope (and falls back to transientNamescope); if both are null the element is effectively orphaned or not attached to a page/ContentView that establishes a namescope.","triggerScenarios":"Calling element.FindByName(name) on an Element that has not been added to the visual/logical tree, or whose parent chain contains no element with a NameScope attached property set. Common with freshly constructed elements not yet parented, or elements inside dynamically created subtrees where SetNameScope was never called.","commonSituations":"Calling FindByName in a constructor before the element is parented to a Page; accessing elements inside a DataTemplate from outside the template's namescope boundary; elements created in code that were never registered with a NameScope via NameScope.SetNameScope(parent, new NameScope()).","solutions":["Ensure the element is parented into a page or ContentView before calling FindByName (wait for the Parented event or handler attachment).","If creating an element tree in code, explicitly set a namescope: NameScope.SetNameScope(rootElement, new NameScope());","Cast to INameScope and register the element, or store a direct reference instead of relying on name lookup.","Check element.GetNameScope() is non-null before calling FindByName."],"exampleFix":"// before\nvar child = element.FindByName(\"myLabel\"); // throws if element has no namescope\n// after\nNameScope.SetNameScope(root, new NameScope());\n((INameScope)root).RegisterName(\"myLabel\", label);\nvar child = root.FindByName(\"myLabel\");","handlingStrategy":"validation","validationCode":"var ns = (element as INameScope) != null ? element.GetNameScope() : null;\nif (ns == null && (element as Element)?.transientNamescope == null)\n    // element is not in a namescope; do not call FindByName","typeGuard":"static bool IsInNameScope(Element e) => e.GetNameScope() != null ||\n    typeof(Element).GetField(\"transientNamescope\",\n        System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)\n        ?.GetValue(e) != null;","tryCatchPattern":"try { return element.FindByName(name); }\ncatch (InvalidOperationException ex) when (ex.Message == \"this element is not in a namescope\")\n{\n    return null; // or fall back to visual-tree search\n}","preventionTips":["Only call FindByName on elements parented into a Page or ContentView.","Establish a namescope on code-built roots with NameScope.SetNameScope.","Listen for the Parented event before performing name lookups."],"tags":["maui","namescope","element-tree","findbyname","invalidoperation"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}