unoplatform/uno · error · XamlGenerationException

The name '{value}' is already defined in this scope

Error message

The name '{value}' is already defined in this scope

What it means

Thrown by ValidateName when the name is a valid identifier but is already present in CurrentScope.DeclaredNames — i.e. two elements in the same namescope share an x:Name. Duplicate names would collide the generated backing fields and break FindName resolution at runtime.

Source

Thrown at src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs:3741

					writer.AppendIndented($"{closure}.{member.Member.Name} = {propertyValue};\r\n");
				}
			}
		}

		private static bool IsNotFrameworkElementButNeedsSourceLocation(XamlObjectDefinition objectDefinition)
			=> objectDefinition.Type.Name is "VisualState" or "AdaptiveTrigger" or "StateTrigger";

		private void ValidateName(string? value, IXamlLocation location)
		{
			// Nullable suppressions are due to https://github.com/dotnet/roslyn/issues/55507
			if (!SyntaxFacts.IsValidIdentifier(value!))
			{
				throw new XamlGenerationException($"The value '{value}' is an invalid value for 'Name' property", location);
			}

			if (!CurrentScope.DeclaredNames.Add(value!))
			{
				throw new XamlGenerationException($"The name '{value}' is already defined in this scope", location);
			}
		}

		private IPropertySymbol? FindLazyContentProperty(XamlMemberDefinition? implicitContentChild, INamedTypeSymbol? elementType)
		{
			if (implicitContentChild != null && elementType != null)
			{
				var contentProperty = FindContentProperty(elementType);

				if (contentProperty != null && IsLazyVisualStateManagerProperty(contentProperty) && !HasDescendantsWithXName(implicitContentChild))
				{
					return contentProperty;
				}
			}

			return null;
		}

View on GitHub (pinned to 0418340488)

Solutions

  1. Rename one of the colliding elements to a unique x:Name within the scope.
  2. Search the file (and the template) for the name to find all occurrences.
  3. If names legitimately repeat across separate DataTemplates, note each template has its own namescope — but top-level page names must still be unique.

Example fix

<!-- before -->
<StackPanel>
  <TextBox x:Name="Input"/>
  <TextBox x:Name="Input"/>
</StackPanel>

<!-- after -->
<StackPanel>
  <TextBox x:Name="PrimaryInput"/>
  <TextBox x:Name="SecondaryInput"/>
</StackPanel>
Defensive patterns

Strategy: validation

Validate before calling

# Detect duplicate x:Name within the same file scope
Get-ChildItem -Recurse -Filter *.xaml | ForEach-Object {
  $names = Select-String -Path $_.FullName -Pattern 'x:Name="([^"]+)"' |
    ForEach-Object { $_.Matches[0].Groups[1].Value }
  $names | Group-Object | Where-Object { $_.Count -gt 1 } |
    ForEach-Object { Write-Warning "Duplicate x:Name $($_.Name) x$($_.Count)" }
}

Prevention

When it happens

Trigger: Two sibling elements with the same x:Name in the same page/control namescope; copying a block and forgetting to rename; a DataTemplate's element name clashing with the page's.

Common situations: Copy-paste of UI blocks; refactor merging two XAML regions; designer duplicating names automatically.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/bc682663f6a548f4. Report an issue: GitHub.