{"record":{"id":"0fb238f682b2cbf4","repo":"dotnet/maui","slug":"namescopes-are-not-supported-please-enable-the-fe","errorCode":null,"errorMessage":"Namescopes are not supported. Please enable the feature switch 'Microsoft.Maui.RuntimeFeature.AreNamescopesSupported' to keep using namescopes.","messagePattern":"Namescopes are not supported\\. Please enable the feature switch 'Microsoft\\.Maui\\.RuntimeFeature\\.AreNamescopesSupported' to keep using namescopes\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Element/Element.cs","lineNumber":568,"sourceCode":"\t\t/// page, content view, or data template defines its own namescope.\n\t\t/// </para>\n\t\t/// <para>\n\t\t/// The search is limited to elements that have been registered in the same namescope, which includes:\n\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/>","sourceCodeStart":550,"sourceCodeEnd":586,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Element/Element.cs#L550-L586","documentation":"Thrown by Element.FindByName when the runtime feature switch 'Microsoft.Maui.RuntimeFeature.AreNamescopesSupported' evaluates to false. MAUI uses feature switches for trimming/AOT scenarios; namescope support (x:Name lookup, INameScope registration) can be disabled to reduce app size. By default SupportNamescopesByDefault is true, so this only fires when the switch is explicitly turned off in the app's runtimeconfig or .csproj.","triggerScenarios":"Calling element.FindByName(\"name\") on any Element instance while AppContext switch 'Microsoft.Maui.RuntimeFeature.AreNamescopesSupported' is set to false. This happens after the app declares the feature switch as false via runtimeconfig.json, .csproj <RuntimeHostConfigurationOption>, or AppContext.SetSwitch at startup.","commonSituations":"Publishing a MAUI app with AOT/trimming enabled and the feature switch defaulting off; upgrading MAUI versions where the switch default changed; explicitly disabling namescopes in a publish profile to reduce binary size; calling FindByName in code-behind after the XAML compiler has been configured for size optimization.","solutions":["Enable the feature switch in your .csproj: <RuntimeHostConfigurationOption Include=\"Microsoft.Maui.RuntimeFeature.AreNamescopesSupported\" Value=\"true\" />","Check at startup: ensure no code or runtimeconfig.json sets 'Microsoft.Maui.RuntimeFeature.AreNamescopesSupported' to false.","Replace FindByName calls with direct references held from XAML code-behind partial fields, or use VisualDiagnostics/visual-tree traversal that does not depend on namescopes.","If disabling namescopes intentionally, remove all x:Name usages and FindByName/RegisterName calls from your XAML and code-behind."],"exampleFix":"// before\nvar btn = (Button)myElement.FindByName(\"submitButton\");\n// after — enable the switch in .csproj, or reference directly\nvar btn = submitButton; // x:Name partial field still works when switch is on","handlingStrategy":"validation","validationCode":"if (!Microsoft.Maui.RuntimeFeature.AreNamescopesSupported)\n    throw new InvalidOperationException(\"Namescope feature switch is disabled; cannot use FindByName.\");","typeGuard":null,"tryCatchPattern":"try { var found = element.FindByName(name); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"AreNamescopesSupported\"))\n{\n    // namescope feature switch is off; fall back to direct references\n}","preventionTips":["Verify the feature switch value in runtimeconfig before relying on namescopes.","Prefer x:Name code-behind partial fields which compile to direct references where possible.","Document which XAML/code paths depend on namescopes when publishing trimmed apps."],"tags":["maui","namescope","feature-switch","xaml","trimming"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}