unoplatform/uno · error · XamlGenerationException
Cannot assign child of type '{childType}' to property of typ
Error message
Cannot assign child of type '{childType}' to property of type '{propertyType.GetFullyQualifiedTypeExcludingGlobal()}' What it means
Thrown in the property-member handling when a property expects exactly one child object (member.Objects.Count == 1) but the child's resolved type is not assignable to the property's type. The generator emits the child type via GetType and the property type from the member symbol and finds them incompatible, so it refuses to emit an invalid assignment.
Source
Thrown at src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs:3457
// Plain object
writer.AppendLineIndented(
$"var {localCollectionName} = {writer.AppliedParameterName}.{member.Member.Name};"
);
}
foreach (var inner in member.Objects)
{
writer.AppendIndented($"{localCollectionName}.Add(");
BuildChild(writer, member, inner);
writer.AppendLineIndented(");");
}
}
else if (member.Objects.Count == 1)
{
var childType = GetType(member.Objects[0].Type).GetFullyQualifiedTypeExcludingGlobal();
throw new XamlGenerationException($"Cannot assign child of type '{childType}' to property of type '{propertyType.GetFullyQualifiedTypeExcludingGlobal()}'", member.Objects[0]);
}
else
{
throw new XamlGenerationException($"The property '{member.Member.Name}' of type '{propertyType}' does not support adding multiple objects", member);
}
}
else
{
// GenerateWarning(writer, $"Unknown type {objectDefinition.Type} for property {member.Member.DeclaringType}");
}
}
else
{
var isMemberInsideResourceDictionary = IsMemberInsideResourceDictionary(objectDefinition);
var value = member.Value?.ToString();
if (
member.Member.Name == "Name"View on GitHub (pinned to 0418340488)
Solutions
- Check the property's documented type and place an element whose type matches (or is assignable to) it.
- Wrap the content in the expected type (e.g. wrap in <DataTemplate> if a template is required).
- If the property is a collection, ensure the child type implements the collection's element type.
- Verify no namespace collision is resolving the element to an unexpected type.
Example fix
<!-- before -->
<ContentControl>
<ContentControl.Content>
<RowDefinition/> <!-- wrong type -->
</ContentControl.Content>
</ContentControl>
<!-- after -->
<ContentControl>
<ContentControl.Content>
<TextBlock Text="Hello"/>
</ContentControl.Content>
</ContentControl> Defensive patterns
Strategy: type-guard
Validate before calling
# Confirm the element type is assignable to the property type by checking the docs/API
# Pre-build: review each property-element tag and verify the child type matches the property's declared type.
# In code-behind, the equivalent guard is a type check before assignment:
if (child is TargetType typed) { control.Property = typed; } Type guard
// C# type guard for programmatic construction (mirrors what XAML enforces):
static void SetContent(object control, object child)
{
if (control is ContentControl cc && child is UIElement uie)
{
cc.Content = uie;
}
else
{
throw new ArgumentException($"Cannot assign {child?.GetType()} to content");
}
} Prevention
- Look up the property's declared type in the WinUI/Uno API docs before nesting an element.
- Use property-element syntax (<Control.Property>) explicitly so the assignment target is obvious.
- Watch for type renames after framework version bumps.
When it happens
Trigger: Assigning an element of type A to a property typed as unrelated type B, e.g. <Grid.RowDefinitions><RowDefinition/></Grid.RowDefinitions> is fine but <SomeControl.Header><RowDefinition/></SomeControl.Header> is not; nested an object whose type does not match the collection/item type the property expects when there is exactly one object.
Common situations: Wrong element inside a property-element tag; a type that used to be assignable but changed after an update; misunderstanding which type a property accepts (e.g. putting a UIElement where a DataTemplate is required).
Related errors
- The property '{member.Member.Name}' of type '{propertyType}'
- Value must either be null or of type bool. Got {value} ({val
- Value must either be null or of type bool. Got {value} ({val
- Value must either be null or of type bool. Got {value} ({val
- Value must either be null or of type bool. Got {value} ({val
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/35b0a720cfb8b219.
Report an issue: GitHub.