{"record":{"id":"c03f25a18dd77ec5","repo":"dotnet/maui","slug":"xc0064","errorCode":"XC0064","errorMessage":"An element with the name \"{0}\" already exists in this NameScope.","messagePattern":"An element with the name \"(.+?)\" already exists in this NameScope\\.","errorType":"exception","errorClass":"BuildException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Build.Tasks/SetNamescopesAndRegisterNamesVisitor.cs","lineNumber":151,"sourceCode":"\t\tvoid SetNameScope(ElementNode node, VariableDefinition ns)\n\t\t{\n\t\t\tvar module = Context.Body.Method.Module;\n\t\t\tvar parameterTypes = new[] {\n\t\t\t\t(\"Microsoft.Maui.Controls\", \"Microsoft.Maui.Controls\", \"BindableObject\"),\n\t\t\t\t(\"Microsoft.Maui.Controls\", \"Microsoft.Maui.Controls.Internals\", \"INameScope\"),\n\t\t\t};\n\t\t\tContext.IL.Append(Context.Variables[node].LoadAs(Context.Cache, module.GetTypeDefinition(Context.Cache, parameterTypes[0]), module));\n\t\t\tContext.IL.Append(ns.LoadAs(Context.Cache, module.GetTypeDefinition(Context.Cache, parameterTypes[1]), module));\n\t\t\tContext.IL.Emit(OpCodes.Call, module.ImportMethodReference(Context.Cache, (\"Microsoft.Maui.Controls\", \"Microsoft.Maui.Controls.Internals\", \"NameScope\"),\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   methodName: \"SetNameScope\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   parameterTypes: parameterTypes,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   isStatic: true));\n\t\t}\n\n\t\tvoid RegisterName(string str, VariableDefinition namescopeVarDef, IList<string> namesInNamescope, VariableDefinition element, INode node)\n\t\t{\n\t\t\tif (namesInNamescope.Contains(str))\n\t\t\t\tthrow new BuildException(BuildExceptionCode.NamescopeDuplicate, node as IXmlLineInfo, null, str);\n\t\t\tnamesInNamescope.Add(str);\n\n\t\t\tvar module = Context.Body.Method.Module;\n\t\t\tvar namescopeType = (\"Microsoft.Maui.Controls\", \"Microsoft.Maui.Controls.Internals\", \"INameScope\");\n\t\t\tContext.IL.Append(namescopeVarDef.LoadAs(Context.Cache, module.GetTypeDefinition(Context.Cache, namescopeType), module));\n\t\t\tContext.IL.Emit(OpCodes.Ldstr, str);\n\t\t\tContext.IL.Append(element.LoadAs(Context.Cache, module.TypeSystem.Object, module));\n\t\t\tContext.IL.Emit(OpCodes.Callvirt, module.ImportMethodReference(Context.Cache,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   namescopeType,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   methodName: \"RegisterName\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   parameterTypes: new[] {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   (\"mscorlib\", \"System\", \"String\"),\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   (\"mscorlib\", \"System\", \"Object\"),\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   }));\n\t\t}\n\n\t\tvoid SetStyleId(string str, VariableDefinition element)\n\t\t{","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Build.Tasks/SetNamescopesAndRegisterNamesVisitor.cs#L133-L169","documentation":"Thrown by RegisterName when an x:Name (or Name) value has already been registered within the same NameScope during XAML compilation. The compiler keeps a per-NameScope list of names and rejects the second occurrence.","triggerScenarios":"Two elements in the same NameScope share an x:Name/Name, e.g. two buttons both `x:Name=\"myButton\"`.","commonSituations":"Copy-pasted XAML elements that kept the same name; refactoring that merged two XAML fragments; auto-generated names colliding with hand-written ones.","solutions":["Rename one of the elements so every name in the same NameScope is unique.","Remove the duplicate element if it was pasted by mistake.","If names legitimately repeat across scopes, ensure they are in separate NameScopes (e.g. different DataTemplate roots)."],"exampleFix":"<!-- before -->\n<Button x:Name=\"btn\" />\n<Button x:Name=\"btn\" />\n\n<!-- after -->\n<Button x:Name=\"btnOk\" />\n<Button x:Name=\"btnCancel\" />","handlingStrategy":"validation","validationCode":"// Lint: detect duplicate x:Name within a single namescope (file scope here)\nusing System.Xml.Linq;\nstatic IEnumerable<string> DuplicateXNames(string xaml)\n{\n    var names = XElement.Parse(xaml).Descendants()\n        .Select(e => (string)e.Attribute(\"{http://schemas.microsoft.com/winfx/2006/xaml}Name\"))\n        .Where(n => !string.IsNullOrEmpty(n));\n    return names.GroupBy(n => n).Where(g => g.Count() > 1).Select(g => g.Key);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Give every element a unique x:Name within its NameScope.","After copy-pasting XAML, rename pasted elements immediately.","Use a XAML linter/analyzer to catch duplicate names before build."],"tags":["xaml","xamlc","namescope","x-name"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}