unoplatform/uno · error · XamlGenerationException

Dictionary item '{resource.Type?.Name}' has duplicate key '{

Error message

Dictionary item '{resource.Type?.Name}' has duplicate key '{key}' {(theme != null ? $"in theme '{theme}'" : "")}

What it means

Thrown by the resource dictionary key-registration logic when two resources share the same x:Key within the same theme scope. The exception message includes the resource type name, the duplicate key, and the theme (if applicable). This is a XamlGenerationException, meaning it includes location info.

Source

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

			var resources = (resourcesRoot?.Objects).Safe();
			var initializers = new Dictionary<XamlObjectDefinition, string>();

			// Pre-populate initializer names (though this is probably no longer necessary...?)
			var index = _dictionaryPropertyIndex;
			foreach (var resource in resources)
			{
				var key = GetDictionaryResourceKey(resource);

				if (key == null || IsNativeStyle(resource))
				{
					continue;
				}

				index++;
				var propertyName = GetInitializerNameForResourceKey(index);
				if (_topLevelQualifiedKeys.ContainsKey((theme, key)))
				{
					throw new XamlGenerationException($"Dictionary item '{resource.Type?.Name}' has duplicate key '{key}' {(theme != null ? $"in theme '{theme}'" : "")}", resource);
				}
				var isStaticResourceAlias = resource.Type.Name == "StaticResource";
				if (!isStaticResourceAlias)
				{
					_topLevelQualifiedKeys[(theme, key)] = propertyName;
				}
			}

			var former = _themeDictionaryCurrentlyBuilding; //Will 99% of the time be null. (Mainly this is half-heartedly trying to support funky usage of recursive merged dictionaries.)
			_themeDictionaryCurrentlyBuilding = theme;
			foreach (var resource in resources)
			{
				var key = GetDictionaryResourceKey(resource);

				if (key == null || IsNativeStyle(resource))
				{
					continue;
				}

View on GitHub (pinned to 0418340488)

Solutions

  1. Search the XAML for the duplicate key reported in the error and remove or rename one of the entries.
  2. If using merged dictionaries, check all merged sources for conflicting keys.
  3. If the duplicate is intentional across themes, ensure each is in the correct ThemeDictionaries scope.
  4. Use a unique x:Key for each resource.

Example fix

<!-- before -->
<ResourceDictionary>
  <x:String x:Key="MyBrush">Red</x:String>
  <x:String x:Key="MyBrush">Blue</x:String>
</ResourceDictionary>
<!-- after -->
<ResourceDictionary>
  <x:String x:Key="MyBrush">Red</x:String>
  <x:String x:Key="MyBrushAlt">Blue</x:String>
</ResourceDictionary>
Defensive patterns

Strategy: validation

Validate before calling

// Scan for duplicate x:Key values before building:
// In a pre-build linting step, parse XAML resource dictionaries and collect keys:
//   var keys = new HashSet<string>();
//   foreach (var key in allResourceKeys) {
//       if (!keys.Add(key)) Console.WriteLine($"Duplicate key: {key}");
//   }
<!-- In XAML, search: grep 'x:Key="' across all merged dictionaries for collisions -->

Prevention

When it happens

Trigger: Two or more resource entries in the same ResourceDictionary (or across merged dictionaries at the top level) define the same x:Key — e.g. two <x:String x:Key="MyKey"> entries, or a themed resource that collides across Light/Dark themes incorrectly.

Common situations: Copy-paste of a resource without changing the key; merging multiple resource dictionaries that define the same key; theme resources that inadvertently share a key when they should be in separate theme scopes; refactoring that left orphaned duplicate entries.

Related errors


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