Skip to content

Commit e775744

Browse files
Copilotnickrandolph
andcommitted
fix: Handle collection properties in custom markup extensions
Co-authored-by: nickrandolph <1614057+nickrandolph@users.noreply.github.com>
1 parent ff0cd21 commit e775744

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,38 @@ private void ProcessCustomMarkupExtension(object instance, object? rootInstance,
10491049
{
10501050
if (propertyInfo != null)
10511051
{
1052-
GetPropertySetter(propertyInfo).Invoke(instance, new[] { value });
1052+
// Check if this is an initialized collection property (like Panel.Children)
1053+
if (TypeResolver.IsInitializedCollection(propertyInfo))
1054+
{
1055+
if (propertyInfo.GetMethod == null)
1056+
{
1057+
throw new InvalidOperationException($"The property {propertyInfo} does not provide a getter (Line {member.LineNumber}:{member.LinePosition}");
1058+
}
1059+
1060+
var propertyInstance = propertyInfo.GetMethod.Invoke(instance, null);
1061+
1062+
if (propertyInstance != null)
1063+
{
1064+
// Add the value returned by the markup extension to the collection
1065+
var addMethod = propertyInstance.GetType().GetMethod("Add");
1066+
if (addMethod != null)
1067+
{
1068+
addMethod.Invoke(propertyInstance, new[] { value });
1069+
}
1070+
else
1071+
{
1072+
throw new InvalidOperationException($"Unable to find Add method for collection property [{propertyInfo}]");
1073+
}
1074+
}
1075+
else
1076+
{
1077+
throw new InvalidOperationException($"The property {propertyInfo} getter did not provide a value (Line {member.LineNumber}:{member.LinePosition}");
1078+
}
1079+
}
1080+
else
1081+
{
1082+
GetPropertySetter(propertyInfo).Invoke(instance, new[] { value });
1083+
}
10531084
}
10541085
else if (TypeResolver.FindDependencyProperty(member) is { } dependencyProperty &&
10551086
instance is DependencyObject dependencyObject)

0 commit comments

Comments
 (0)