Skip to content

Commit aa190d6

Browse files
Fixed-ItemSpacing-Issue-On-ItemsLayout
1 parent 5fc2590 commit aa190d6

File tree

5 files changed

+152
-14
lines changed

5 files changed

+152
-14
lines changed

src/Controls/src/Core/Handlers/Items2/CollectionViewHandler2.iOS.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#nullable disable
22
using System;
33
using System.Collections.Generic;
4+
using System.ComponentModel;
45
using System.Text;
56
using Foundation;
67
using Microsoft.Maui.Handlers;
@@ -203,20 +204,29 @@ void SubscribeToItemsLayoutPropertyChanged(IItemsLayout itemsLayout)
203204
{
204205
if (itemsLayout is not null)
205206
{
206-
itemsLayout.PropertyChanged += (sender, args) =>
207-
{
208-
if (args.PropertyName == nameof(ItemsLayout.SnapPointsAlignment) ||
209-
args.PropertyName == nameof(ItemsLayout.SnapPointsType) ||
210-
args.PropertyName == nameof(GridItemsLayout.VerticalItemSpacing) ||
211-
args.PropertyName == nameof(GridItemsLayout.HorizontalItemSpacing) ||
212-
args.PropertyName == nameof(GridItemsLayout.Span) ||
213-
args.PropertyName == nameof(LinearItemsLayout.ItemSpacing))
214-
215-
{
216-
UpdateLayout();
217-
}
218-
};
207+
itemsLayout.PropertyChanged -= ItemsLayoutPropertyChanged;
208+
itemsLayout.PropertyChanged += ItemsLayoutPropertyChanged;
209+
}
210+
}
211+
212+
void ItemsLayoutPropertyChanged(object sender, PropertyChangedEventArgs e)
213+
{
214+
if (e.PropertyName == nameof(ItemsLayout.SnapPointsAlignment) ||
215+
e.PropertyName == nameof(ItemsLayout.SnapPointsType) ||
216+
e.PropertyName == nameof(GridItemsLayout.VerticalItemSpacing) ||
217+
e.PropertyName == nameof(GridItemsLayout.HorizontalItemSpacing) ||
218+
e.PropertyName == nameof(GridItemsLayout.Span) ||
219+
e.PropertyName == nameof(LinearItemsLayout.ItemSpacing))
220+
221+
{
222+
UpdateLayout();
219223
}
220224
}
225+
226+
protected override void DisconnectHandler(UIView platformView)
227+
{
228+
ItemsView.ItemsLayout.PropertyChanged -= ItemsLayoutPropertyChanged;
229+
base.DisconnectHandler(platformView);
230+
}
221231
}
222232
}

src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,4 @@ virtual Microsoft.Maui.Controls.Handlers.Items2.ItemsViewHandler2<TItemsView>.Up
349349
*REMOVED*override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.SetNeedsLayout() -> void
350350
*REMOVED*override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.MovedToWindow() -> void
351351
override Microsoft.Maui.Controls.Handlers.Compatibility.VisualElementRenderer<TElement>.MovedToWindow() -> void
352+
~override Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2.DisconnectHandler(UIKit.UIView platformView) -> void

src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,5 @@ virtual Microsoft.Maui.Controls.Handlers.Items2.ItemsViewDelegator2<TItemsView,
348348
virtual Microsoft.Maui.Controls.Handlers.Items2.ItemsViewHandler2<TItemsView>.UpdateLayout() -> void
349349
*REMOVED*override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.SetNeedsLayout() -> void
350350
*REMOVED*override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.MovedToWindow() -> void
351-
override Microsoft.Maui.Controls.Handlers.Compatibility.VisualElementRenderer<TElement>.MovedToWindow() -> void
351+
override Microsoft.Maui.Controls.Handlers.Compatibility.VisualElementRenderer<TElement>.MovedToWindow() -> void
352+
~override Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2.DisconnectHandler(UIKit.UIView platformView) -> void
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Maui.Controls.Sample.Issues;
4+
5+
[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)]
6+
7+
public class Issue27666_NavigationPage : TestNavigationPage
8+
{
9+
protected override void Init()
10+
{
11+
var root = CreateRootContentPage();
12+
PushAsync(root);
13+
}
14+
15+
ContentPage CreateRootContentPage()
16+
{
17+
ContentPage ContentPage = new ContentPage();
18+
19+
VerticalStackLayout rootLayout = new VerticalStackLayout
20+
{
21+
Spacing = 10,
22+
Padding = new Thickness(10),
23+
};
24+
25+
Button collectionViewButton = new Button
26+
{
27+
Text = "Navigate to VerticalList CollectionView",
28+
AutomationId = "NavigationButton"
29+
};
30+
collectionViewButton.Clicked += (s, e) => Navigation.PushAsync(new Issue27666());
31+
32+
rootLayout.Add(collectionViewButton);
33+
ContentPage.Content = rootLayout;
34+
return ContentPage;
35+
}
36+
}
37+
38+
public partial class Issue27666 : TestContentPage
39+
{
40+
ObservableCollection<string> items;
41+
CollectionView collectionView;
42+
protected override void Init()
43+
{
44+
items = new ObservableCollection<string>(Enumerable.Range(1, 30).Select(i => $"Item {i}"));
45+
Button itemSpacingButton = CreateButton("Update ItemSpacing", "UpdateItemSpacingButton", OnItemSpacingButtonClicked);
46+
47+
collectionView = new CollectionView
48+
{
49+
AutomationId = "ItemsLayoutCollectionView",
50+
ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal),
51+
ItemsSource = items,
52+
ItemTemplate = new DataTemplate(() =>
53+
{
54+
var label = new Label();
55+
label.SetBinding(Label.TextProperty, ".");
56+
return new Border
57+
{
58+
Content = label,
59+
Padding = 10,
60+
Margin = new Thickness(5),
61+
BackgroundColor = Colors.LightGray,
62+
};
63+
})
64+
};
65+
66+
Grid grid = new Grid
67+
{
68+
RowDefinitions =
69+
{
70+
new RowDefinition { Height = GridLength.Auto },
71+
new RowDefinition { Height = GridLength.Star }
72+
},
73+
RowSpacing = 5
74+
};
75+
grid.Add(itemSpacingButton, 0, 0);
76+
grid.Add(collectionView, 0, 1);
77+
78+
Content = grid;
79+
}
80+
81+
Button CreateButton(string text, string automationId, EventHandler onClick)
82+
{
83+
return new Button
84+
{
85+
Text = text,
86+
AutomationId = automationId,
87+
Command = new Command(_ => onClick(this, EventArgs.Empty))
88+
};
89+
}
90+
91+
void OnItemSpacingButtonClicked(object sender, EventArgs e)
92+
{
93+
if (collectionView.ItemsLayout is LinearItemsLayout layout)
94+
{
95+
layout.ItemSpacing = layout.ItemSpacing == 0 ? 50 : 20;
96+
}
97+
}
98+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue27666 : _IssuesUITest
8+
{
9+
public override string Issue => "Vertical list and Vertical grid pages have different abnormal behaviors when clicking Update after changing the spacing value";
10+
11+
public Issue27666(TestDevice device) : base(device) { }
12+
13+
[Test]
14+
[Category(UITestCategories.CollectionView)]
15+
public void VerifySpacingUpdateInItemsLayout()
16+
{
17+
App.WaitForElement("NavigationButton");
18+
App.Tap("NavigationButton");
19+
App.WaitForElement("ItemsLayoutCollectionView");
20+
App.Tap("UpdateItemSpacingButton");
21+
App.TapBackArrow();
22+
App.WaitForElement("NavigationButton");
23+
App.Tap("NavigationButton");
24+
App.WaitForElement("ItemsLayoutCollectionView");
25+
App.Tap("UpdateItemSpacingButton");
26+
App.WaitForElement("ItemsLayoutCollectionView");
27+
}
28+
}

0 commit comments

Comments
 (0)