unoplatform/uno · error · XamlGenerationException

Each dictionary entry must have a 'Key'

Error message

Each dictionary entry must have a 'Key'

What it means

Thrown in the same dictionary loop when isDict is true but GetDictionaryResourceKey returns null for an entry — i.e. a dictionary entry has no x:Key (and no x:Name fallback that the key resolver accepts). Every entry in a keyed ResourceDictionary requires a key so the generator can emit it into the generated dictionary initializer.

Source

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

			}
			else
			{
				writer.AppendLineInvariantIndented("// {0}", propertyName);
			}

			for (int i = 0; i < dictionaries.Objects.Count; i++)
			{
				var dictObject = dictionaries.Objects[i];
				var source = dictObject.Members.FirstOrDefault(m => m.Member.Name == "Source");
				if (source != null && dictObject.Members.Any(m => m.Member.Name == "_UnknownContent"))
				{
					throw new XamlGenerationException("Local values are not allowed in resource dictionary with Source set", dictObject);
				}

				var key = GetDictionaryResourceKey(dictObject);
				if (isDict && key == null)
				{
					throw new XamlGenerationException("Each dictionary entry must have a 'Key'", dictObject);
				}

				var former = _themeDictionaryCurrentlyBuilding; //Will 99% of the time be null.
				if (isDict)
				{
					_themeDictionaryCurrentlyBuilding = key;
				}

				if (!isSetExtensionMethod)
				{
					if (!isInInitializer && !isDict)
					{
						writer.AppendLineInvariantIndented("{0}.{1}.Add(", dictIdentifier, propertyName);
					}
					else if (!isInInitializer && isDict)
					{
						writer.AppendLineInvariantIndented("{0}.{1}[\"{2}\"] = ", dictIdentifier, propertyName, key);
					}

View on GitHub (pinned to 0418340488)

Solutions

  1. Add x:Key="someKey" to every entry directly inside a ResourceDictionary.
  2. For a Style, add x:Key even if TargetType is set, unless the style is intentionally implicit (in which case ensure it is in the right implicit-style location).
  3. If the entry is meant to be merged from elsewhere, move it under MergedDictionaries with a Source.

Example fix

<!-- before -->
<ResourceDictionary>
  <SolidColorBrush Color="Red"/>
</ResourceDictionary>

<!-- after -->
<ResourceDictionary>
  <SolidColorBrush x:Key="RedBrush" Color="Red"/>
</ResourceDictionary>
Defensive patterns

Strategy: validation

Validate before calling

# Flag elements inside <ResourceDictionary> (not MergedDictionaries) lacking x:Key
Get-ChildItem -Recurse -Filter *.xaml | ForEach-Object {
  $x = [xml](Get-Content $_.FullName)
  $x.SelectNodes('//*[local-name()="ResourceDictionary"]/*') | Where-Object {
    $_.LocalName -ne 'ResourceDictionary.MergedDictionaries' -and -not $_.HasAttribute('Key','http://schemas.microsoft.com/winfx/2006/xaml')
  } | ForEach-Object { Write-Warning "Entry $($_.LocalName) missing x:Key" }
}

Prevention

When it happens

Trigger: An element inside a <ResourceDictionary> (not a MergedDictionaries child) lacking x:Key; a resource relying on an implicit key (by TargetType in a Style) which Uno does not infer in this context.

Common situations: Porting WPF styles that use implicit Style keys (Style keyed by its TargetType) into a context where the generator requires an explicit key; forgetting x:Key on a brush/style added in haste.

Related errors


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