Skip to content

Commit 42ca2bd

Browse files
authored
[iOS] CarouselView with CarouselViewHandler2 make app crash when Loop="False" and user scroll to the last position - fix (#26868)
* Update CarouselViewController2.cs * Added a UI test for Matt
1 parent 5442a5e commit 42ca2bd

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public override UICollectionViewCell GetCell(UICollectionView collectionView, NS
4949
return cell;
5050
}
5151

52-
public override nint GetItemsCount(UICollectionView collectionView, nint section) => LoopItemsSource.LoopCount;
52+
public override nint GetItemsCount(UICollectionView collectionView, nint section) => LoopItemsSource.Loop ? LoopItemsSource.LoopCount : LoopItemsSource.ItemCount;
5353

5454
void InitializeCarouselViewLoopManager()
5555
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Maui.Controls.Sample.Issues.Issue26863"
5+
x:Name="This">
6+
7+
<VerticalStackLayout BindingContext="{Binding Source={Reference This}}">
8+
<Button AutomationId="button"
9+
x:Name="button"
10+
IsVisible="false"
11+
Text="Click me"
12+
Command="{Binding ScrollToCommand}"/>
13+
<CarouselView
14+
HeightRequest="200"
15+
ItemsSource="{Binding List}"
16+
HorizontalOptions="Fill"
17+
x:Name="CV"
18+
Loop="False">
19+
<CarouselView.ItemTemplate>
20+
<DataTemplate>
21+
<Label HorizontalOptions="Fill"
22+
AutomationId="{Binding .}"
23+
Text="{Binding .}"/>
24+
</DataTemplate>
25+
</CarouselView.ItemTemplate>
26+
</CarouselView>
27+
</VerticalStackLayout>
28+
29+
</ContentPage>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Maui.Controls.Sample.Issues
4+
{
5+
[Issue(IssueTracker.Github, 26863, "CarouselView with CarouselViewHandler2 make app crash", PlatformAffected.iOS)]
6+
public partial class Issue26863 : ContentPage
7+
{
8+
public Issue26863()
9+
{
10+
InitializeComponent();
11+
}
12+
13+
public ObservableCollection<string> List { get; } = new();
14+
public Command ScrollToCommand => new(() => CV.ScrollTo(2, 0, animate: true));
15+
16+
protected override void OnAppearing()
17+
{
18+
List.Clear();
19+
List.Add("item1");
20+
List.Add("item2");
21+
List.Add("item3");
22+
button.IsVisible = true;
23+
}
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues
6+
{
7+
public class Issue26863 : _IssuesUITest
8+
{
9+
public Issue26863(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "CarouselView with CarouselViewHandler2 make app crash";
14+
15+
[Test]
16+
[Category(UITestCategories.CarouselView)]
17+
public void AppShouldNotCrashOnScrollingCarouselViewWithoutLoop()
18+
{
19+
App.WaitForElement("button");
20+
App.Click("button");
21+
22+
//The test passes if no crash is observed
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)