diff --git a/src/Controls/src/Core/Handlers/Items2/CollectionViewHandler2.iOS.cs b/src/Controls/src/Core/Handlers/Items2/CollectionViewHandler2.iOS.cs index c9507bc884ba..5c7a3b780ac4 100644 --- a/src/Controls/src/Core/Handlers/Items2/CollectionViewHandler2.iOS.cs +++ b/src/Controls/src/Core/Handlers/Items2/CollectionViewHandler2.iOS.cs @@ -1,6 +1,7 @@ #nullable disable using System; using System.Collections.Generic; +using System.ComponentModel; using System.Text; using Foundation; using Microsoft.Maui.Handlers; @@ -203,20 +204,29 @@ void SubscribeToItemsLayoutPropertyChanged(IItemsLayout itemsLayout) { if (itemsLayout is not null) { - itemsLayout.PropertyChanged += (sender, args) => - { - if (args.PropertyName == nameof(ItemsLayout.SnapPointsAlignment) || - args.PropertyName == nameof(ItemsLayout.SnapPointsType) || - args.PropertyName == nameof(GridItemsLayout.VerticalItemSpacing) || - args.PropertyName == nameof(GridItemsLayout.HorizontalItemSpacing) || - args.PropertyName == nameof(GridItemsLayout.Span) || - args.PropertyName == nameof(LinearItemsLayout.ItemSpacing)) - - { - UpdateLayout(); - } - }; + itemsLayout.PropertyChanged -= ItemsLayoutPropertyChanged; + itemsLayout.PropertyChanged += ItemsLayoutPropertyChanged; + } + } + + void ItemsLayoutPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == nameof(ItemsLayout.SnapPointsAlignment) || + e.PropertyName == nameof(ItemsLayout.SnapPointsType) || + e.PropertyName == nameof(GridItemsLayout.VerticalItemSpacing) || + e.PropertyName == nameof(GridItemsLayout.HorizontalItemSpacing) || + e.PropertyName == nameof(GridItemsLayout.Span) || + e.PropertyName == nameof(LinearItemsLayout.ItemSpacing)) + + { + UpdateLayout(); } } + + protected override void DisconnectHandler(UIView platformView) + { + ItemsView.ItemsLayout.PropertyChanged -= ItemsLayoutPropertyChanged; + base.DisconnectHandler(platformView); + } } } diff --git a/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt index 7a5d90a5f28f..84545da6b5fc 100644 --- a/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt @@ -14,4 +14,5 @@ virtual Microsoft.Maui.Controls.BindableProperty.CreateDefaultValueDelegate.Invoke(Microsoft.Maui.Controls.BindableObject bindable, TPropertyType value) -> bool ~virtual Microsoft.Maui.Controls.CollectionSynchronizationCallback.Invoke(System.Collections.IEnumerable collection, object context, System.Action accessMethod, bool writeAccess) -> void ~virtual Microsoft.Maui.Controls.Internals.EvaluateJavaScriptDelegate.Invoke(string script) -> System.Threading.Tasks.Task -~virtual Microsoft.Maui.Controls.PropertyChangingEventHandler.Invoke(object sender, Microsoft.Maui.Controls.PropertyChangingEventArgs e) -> void \ No newline at end of file +~virtual Microsoft.Maui.Controls.PropertyChangingEventHandler.Invoke(object sender, Microsoft.Maui.Controls.PropertyChangingEventArgs e) -> void +~override Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2.DisconnectHandler(UIKit.UIView platformView) -> void \ No newline at end of file diff --git a/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt index 7a5d90a5f28f..84545da6b5fc 100644 --- a/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt @@ -14,4 +14,5 @@ virtual Microsoft.Maui.Controls.BindableProperty.CreateDefaultValueDelegate.Invoke(Microsoft.Maui.Controls.BindableObject bindable, TPropertyType value) -> bool ~virtual Microsoft.Maui.Controls.CollectionSynchronizationCallback.Invoke(System.Collections.IEnumerable collection, object context, System.Action accessMethod, bool writeAccess) -> void ~virtual Microsoft.Maui.Controls.Internals.EvaluateJavaScriptDelegate.Invoke(string script) -> System.Threading.Tasks.Task -~virtual Microsoft.Maui.Controls.PropertyChangingEventHandler.Invoke(object sender, Microsoft.Maui.Controls.PropertyChangingEventArgs e) -> void \ No newline at end of file +~virtual Microsoft.Maui.Controls.PropertyChangingEventHandler.Invoke(object sender, Microsoft.Maui.Controls.PropertyChangingEventArgs e) -> void +~override Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2.DisconnectHandler(UIKit.UIView platformView) -> void \ No newline at end of file diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue27666.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue27666.cs new file mode 100644 index 000000000000..83f5d0064141 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue27666.cs @@ -0,0 +1,56 @@ +using System.Collections.ObjectModel; + +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 27666, "Vertical list and Vertical grid pages have different abnormal behaviors when clicking Update after changing the spacing value", PlatformAffected.iOS | PlatformAffected.macOS)] + +public class Issue27666_NavigationPage : TestNavigationPage +{ + protected override void Init() + { + var root = CreateRootContentPage(); + PushAsync(root); + } + + ContentPage CreateRootContentPage() + { + ContentPage ContentPage = new ContentPage(); + + VerticalStackLayout rootLayout = new VerticalStackLayout + { + Spacing = 10, + Padding = new Thickness(10), + }; + + Button collectionViewButton = new Button + { + Text = "Navigate to VerticalList CollectionView", + AutomationId = "NavigationButton" + }; + collectionViewButton.Clicked += (s, e) => Navigation.PushAsync(new Issue27666()); + + rootLayout.Add(collectionViewButton); + ContentPage.Content = rootLayout; + return ContentPage; + } +} + +public partial class Issue27666 : ContentPage +{ + ObservableCollection items; + + public Issue27666() + { + InitializeComponent(); + items = new ObservableCollection(Enumerable.Range(1, 30).Select(i => $"Item {i}")); + collectionView.ItemsSource = items; + } + + private void OnItemSpacingButtonClicked(object sender, EventArgs e) + { + if (collectionView.ItemsLayout is LinearItemsLayout layout) + { + layout.ItemSpacing = layout.ItemSpacing == 0 ? 50 : 20; + } + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue27666.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue27666.xaml new file mode 100644 index 000000000000..91fc46603b74 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue27666.xaml @@ -0,0 +1,32 @@ + + + + + + + + + +