dotnet/maui · error · BuildException

XC0064

XC0064

Error message

An element with the name "{0}" already exists in this NameScope.

What it means

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.

Source

Thrown at src/Controls/src/Build.Tasks/SetNamescopesAndRegisterNamesVisitor.cs:151

		void SetNameScope(ElementNode node, VariableDefinition ns)
		{
			var module = Context.Body.Method.Module;
			var parameterTypes = new[] {
				("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "BindableObject"),
				("Microsoft.Maui.Controls", "Microsoft.Maui.Controls.Internals", "INameScope"),
			};
			Context.IL.Append(Context.Variables[node].LoadAs(Context.Cache, module.GetTypeDefinition(Context.Cache, parameterTypes[0]), module));
			Context.IL.Append(ns.LoadAs(Context.Cache, module.GetTypeDefinition(Context.Cache, parameterTypes[1]), module));
			Context.IL.Emit(OpCodes.Call, module.ImportMethodReference(Context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls.Internals", "NameScope"),
																	   methodName: "SetNameScope",
																	   parameterTypes: parameterTypes,
																	   isStatic: true));
		}

		void RegisterName(string str, VariableDefinition namescopeVarDef, IList<string> namesInNamescope, VariableDefinition element, INode node)
		{
			if (namesInNamescope.Contains(str))
				throw new BuildException(BuildExceptionCode.NamescopeDuplicate, node as IXmlLineInfo, null, str);
			namesInNamescope.Add(str);

			var module = Context.Body.Method.Module;
			var namescopeType = ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls.Internals", "INameScope");
			Context.IL.Append(namescopeVarDef.LoadAs(Context.Cache, module.GetTypeDefinition(Context.Cache, namescopeType), module));
			Context.IL.Emit(OpCodes.Ldstr, str);
			Context.IL.Append(element.LoadAs(Context.Cache, module.TypeSystem.Object, module));
			Context.IL.Emit(OpCodes.Callvirt, module.ImportMethodReference(Context.Cache,
																		   namescopeType,
																		   methodName: "RegisterName",
																		   parameterTypes: new[] {
																			   ("mscorlib", "System", "String"),
																			   ("mscorlib", "System", "Object"),
																		   }));
		}

		void SetStyleId(string str, VariableDefinition element)
		{

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Rename one of the elements so every name in the same NameScope is unique.
  2. Remove the duplicate element if it was pasted by mistake.
  3. If names legitimately repeat across scopes, ensure they are in separate NameScopes (e.g. different DataTemplate roots).

Example fix

<!-- before -->
<Button x:Name="btn" />
<Button x:Name="btn" />

<!-- after -->
<Button x:Name="btnOk" />
<Button x:Name="btnCancel" />
Defensive patterns

Strategy: validation

Validate before calling

// Lint: detect duplicate x:Name within a single namescope (file scope here)
using System.Xml.Linq;
static IEnumerable<string> DuplicateXNames(string xaml)
{
    var names = XElement.Parse(xaml).Descendants()
        .Select(e => (string)e.Attribute("{http://schemas.microsoft.com/winfx/2006/xaml}Name"))
        .Where(n => !string.IsNullOrEmpty(n));
    return names.GroupBy(n => n).Where(g => g.Count() > 1).Select(g => g.Key);
}

Prevention

When it happens

Trigger: Two elements in the same NameScope share an x:Name/Name, e.g. two buttons both `x:Name="myButton"`.

Common situations: Copy-pasted XAML elements that kept the same name; refactoring that merged two XAML fragments; auto-generated names colliding with hand-written ones.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/c03f25a18dd77ec5. Report an issue: GitHub.