diff --git a/src/Uno.UI/UI/Xaml/UIElement.skia.cs b/src/Uno.UI/UI/Xaml/UIElement.skia.cs index 06badd49e820..477d565b5aad 100644 --- a/src/Uno.UI/UI/Xaml/UIElement.skia.cs +++ b/src/Uno.UI/UI/Xaml/UIElement.skia.cs @@ -206,6 +206,11 @@ internal UIElement ReplaceChild(int index, UIElement child) internal void ClearChildren() { + if (_children.Count == 0) + { + return; + } + foreach (var child in _children.ToArray()) { InnerRemoveChild(child); diff --git a/src/Uno.UI/UI/Xaml/UIElementCollection.cs b/src/Uno.UI/UI/Xaml/UIElementCollection.cs index 62a076a5d1be..1197f905b424 100644 --- a/src/Uno.UI/UI/Xaml/UIElementCollection.cs +++ b/src/Uno.UI/UI/Xaml/UIElementCollection.cs @@ -70,8 +70,10 @@ public void Clear() // This block is a manual enumeration to avoid the foreach pattern // See https://github.com/dotnet/runtime/issues/56309 for details var itemsEnumerator = items.GetEnumerator(); + var hasItems = false; while (itemsEnumerator.MoveNext()) { + hasItems = true; var item = itemsEnumerator.Current; if (item is IDependencyObjectStoreProvider provider) @@ -85,6 +87,11 @@ public void Clear() fe.InvalidateMeasure(); } + if (!hasItems) + { + return; + } + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items.ToList())); }