Skip to content

Commit 1b8101a

Browse files
[Windows] Fix for ItemsSource Does Not Dynamically Set to null in CarouselView (#29660)
* [Windows] Fix for ItemsSource Does Not Dynamically Set to null in CarouselView * Have added test case * Updated AutomationId for better clarity and readability * Have modified the context of the description label * Have committed the snapshots
1 parent 166a865 commit 1b8101a

7 files changed

Lines changed: 90 additions & 6 deletions

File tree

src/Controls/src/Core/Handlers/Items/CarouselViewHandler.Windows.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ protected override void DisconnectHandler(ListViewBase platformView)
6868

6969
protected override void UpdateItemsSource()
7070
{
71-
var itemsSource = ItemsView?.ItemsSource;
72-
73-
if (itemsSource == null)
74-
return;
75-
7671
var itemTemplate = ItemsView?.ItemTemplate;
7772

7873
if (itemTemplate == null)
@@ -149,7 +144,7 @@ protected override ItemsViewScrolledEventArgs ComputeVisibleIndexes(ItemsViewScr
149144
{
150145
args = base.ComputeVisibleIndexes(args, orientation, advancing);
151146

152-
if (ItemsView.Loop)
147+
if (ItemsView.Loop && ItemsView.ItemsSource is not null)
153148
{
154149
args.FirstVisibleItemIndex %= ItemCount;
155150
args.CenterItemIndex %= ItemCount;
@@ -513,6 +508,9 @@ void CarouselScrolled(object sender, ItemsViewScrolledEventArgs e)
513508

514509
void OnScrollViewChanging(object sender, ScrollViewerViewChangingEventArgs e)
515510
{
511+
if (ItemsView.ItemsSource is null)
512+
return;
513+
516514
ItemsView.SetIsDragging(true);
517515
ItemsView.IsScrolling = true;
518516
}
48.3 KB
Loading
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
namespace Controls.TestCases.HostApp.Issues;
2+
3+
[Issue(IssueTracker.Github, 29472, "ItemsSource is not dynamically cleared in the CarouselView", PlatformAffected.UWP)]
4+
public class Issue29472 : ContentPage
5+
{
6+
public Issue29472()
7+
{
8+
Label descriptionLabel = new Label
9+
{
10+
Text = "The test passes if the previously bound items are cleared from the view when the ItemsSource is dynamically set to null; otherwise, it fails.",
11+
FontSize = 16,
12+
HorizontalOptions = LayoutOptions.Center
13+
};
14+
15+
CarouselView carouselView = new CarouselView
16+
{
17+
ItemsSource = new List<string> { "Item1", "Item2" },
18+
Loop = false,
19+
ItemTemplate = new DataTemplate(() =>
20+
{
21+
Label label = new Label
22+
{
23+
FontSize = 24,
24+
BackgroundColor = Colors.LightGray,
25+
Margin = new Thickness(10)
26+
};
27+
28+
label.SetBinding(Label.TextProperty, ".");
29+
return label;
30+
}),
31+
};
32+
33+
Button clearItemsSource = new Button
34+
{
35+
AutomationId = "ClearItemsSourceBtn",
36+
Text = "Set the ItemsSource of the CarouselView to null",
37+
HorizontalOptions = LayoutOptions.Center
38+
};
39+
40+
clearItemsSource.Clicked += (s, e) =>
41+
{
42+
carouselView.ItemsSource = null;
43+
};
44+
45+
Grid grid = new Grid
46+
{
47+
Padding = 20,
48+
RowSpacing = 15,
49+
RowDefinitions =
50+
{
51+
new RowDefinition { Height = GridLength.Auto },
52+
new RowDefinition { Height = GridLength.Auto },
53+
new RowDefinition { Height = GridLength.Star },
54+
}
55+
};
56+
57+
grid.Add(descriptionLabel, 0, 0);
58+
grid.Add(clearItemsSource, 0, 1);
59+
grid.Add(carouselView, 0, 2);
60+
61+
Content = grid;
62+
}
63+
}
26.6 KB
Loading
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue29472 : _IssuesUITest
8+
{
9+
public Issue29472(TestDevice device) : base(device)
10+
{
11+
}
12+
13+
public override string Issue => "ItemsSource is not dynamically cleared in the CarouselView";
14+
15+
[Test]
16+
[Category(UITestCategories.CarouselView)]
17+
public void VerifyCarouselViewItemsSourceClearedDynamically()
18+
{
19+
App.WaitForElement("ClearItemsSourceBtn");
20+
App.Tap("ClearItemsSourceBtn");
21+
VerifyScreenshot();
22+
}
23+
}
11.9 KB
Loading
54.1 KB
Loading

0 commit comments

Comments
 (0)