From 62de06b3ba1ddd6d26d3752b0ca27ad498a857da Mon Sep 17 00:00:00 2001 From: Alberto Aldegheri Date: Sat, 7 Sep 2024 16:24:37 +0200 Subject: [PATCH] Trigger InvalidateMeasure event on CollectionView when content size changes on iOS --- .../Core/Handlers/Items/iOS/ItemsViewController.cs | 6 +++--- .../Elements/CollectionView/CollectionViewTests.cs | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewController.cs b/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewController.cs index 56788d45ebd3..71b2086a82bf 100644 --- a/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewController.cs +++ b/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewController.cs @@ -250,17 +250,17 @@ void InvalidateMeasureIfContentSizeChanged() { invalidate = true; } - - if (heightChanged && (contentSize.Value.Height < screenHeight || _previousContentSize.Height < screenHeight)) + else if (heightChanged && (contentSize.Value.Height < screenHeight || _previousContentSize.Height < screenHeight)) { invalidate = true; } if (invalidate) { - (ItemsView as IView)?.InvalidateMeasure(); + ItemsView.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); } } + _previousContentSize = contentSize.Value; } diff --git a/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.cs b/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.cs index 02b135d24699..8de2746708c5 100644 --- a/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.cs @@ -4,6 +4,7 @@ using System.Collections.ObjectModel; using System.Linq; using System.Reflection; +using System.Threading; using System.Threading.Tasks; using System.Xml.Linq; using Microsoft.Maui.Controls; @@ -375,8 +376,13 @@ public async Task CollectionViewCanSizeToContent(CollectionViewSizingTestCase te var frame = collectionView.Frame; + var measureInvalidatedCount = 0; + void OnCollectionViewOnMeasureInvalidated(object s, EventArgs e) => Interlocked.Increment(ref measureInvalidatedCount); + await CreateHandlerAndAddToWindow(layout, async handler => { + collectionView.MeasureInvalidated += OnCollectionViewOnMeasureInvalidated; + for (int n = 0; n < itemCounts.Length; n++) { int itemsCount = itemCounts[n]; @@ -403,6 +409,13 @@ await CreateHandlerAndAddToWindow(layout, async handler => ? containerHeight : Math.Min(itemsCount * templateHeight, containerHeight); +#if IOS + if (layoutOptions != LayoutOptions.Fill) + { + Assert.Equal(n + 1, measureInvalidatedCount); + } +#endif + if (itemsLayout.Orientation == ItemsLayoutOrientation.Horizontal) { Assert.Equal(expectedWidth, collectionView.Width, tolerance);