unoplatform/uno · error · XamlGenerationException

There is already a resource with name '{name}'

Error message

There is already a resource with name '{name}'

What it means

Thrown in BuildNamedResources when a resource (an entry in a ResourceDictionary or top-level resource) uses an x:Key or x:Name that already exists in the _namedResources map (line 62). The check at line 2881 only applies when _isTopLevelDictionary is false — i.e. inside a resource block within a page/control. Duplicate keys would produce ambiguous generated backing-field names, so generation aborts.

Source

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

					else
					{
						using (TryTernaryForLinkerHint(writer, resource))
						{
							using (BuildLazyResourceInitializer(writer))
							{
								BuildChild(writer, null, resource);
							}
						}
					}

					writer.AppendLineIndented(closingPunctuation);
				}

				if (name != null && !_isTopLevelDictionary)
				{
					if (_namedResources.ContainsKey(name))
					{
						throw new XamlGenerationException($"There is already a resource with name '{name}'", resource);
					}
					_namedResources.Add(name, resource);
				}
			}
		}

		private IDisposable TryTernaryForLinkerHint(IIndentedStringBuilder writer, XamlObjectDefinition resource)
			=> TryLinkerHint(
				resource,
				s => writer.AppendLineIndented($"{s} ? ("),
				() => writer.AppendLineIndented("): null")
			);

		private IDisposable TrySingleLineIfForLinkerHint(IIndentedStringBuilder writer, XamlObjectDefinition resource)
			=> TryLinkerHint(
				resource,
				s => writer.AppendLineIndented($"if({s})"),
				() => { }

View on GitHub (pinned to 0418340488)

Solutions

  1. Search the file (and merged dictionaries at that scope) for the duplicate key and rename one occurrence.
  2. If the duplicate is intentional across separate MergedDictionaries, move them into separate ResourceDictionary files referenced via MergedDictionaries (top-level dictionaries bypass this check).
  3. Use distinct, descriptive keys (e.g. HeaderBrush vs FooterBrush) to avoid accidental collisions.

Example fix

<!-- before -->
<Page.Resources>
  <SolidColorBrush x:Key="Accent" Color="Red"/>
  <SolidColorBrush x:Key="Accent" Color="Blue"/>
</Page.Resources>

<!-- after -->
<Page.Resources>
  <SolidColorBrush x:Key="AccentPrimary" Color="Red"/>
  <SolidColorBrush x:Key="AccentSecondary" Color="Blue"/>
</Page.Resources>
Defensive patterns

Strategy: validation

Validate before calling

# Detect duplicate x:Key within the same ResourceDictionary block
Get-ChildItem -Recurse -Filter *.xaml | ForEach-Object {
  $text = Get-Content $_.FullName -Raw
  $keys = [regex]::Matches($text, 'x:Key="([^"]+)"') | ForEach-Object { $_.Groups[1].Value }
  $dup = $keys | Group-Object | Where-Object { $_.Count -gt 1 }
  $dup | ForEach-Object { Write-Warning "$($_.Name): duplicate x:Key in $($_.Group[0])" }
}

Prevention

When it happens

Trigger: Two resources in the same (non-top-level) ResourceDictionary with the same x:Key; a resource whose x:Name collides with another resource's key; merging inline resources where both define "MyBrush".

Common situations: Copy-pasting a resource and forgetting to rename its key; splitting a large dictionary and introducing a duplicate; merging dictionaries inline that share keys at the same scope.

Related errors


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