Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ internal void UpdateLoop()

void ScrollToPosition(int goToPosition, int carouselPosition, bool animate, bool forceScroll = false)
{
if (goToPosition < 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to skipping negative indices, guard against goToPosition values ≥ item count to avoid accidental out-of-range requests when the source changes between resolve and scroll.

{
System.Diagnostics.Debug.WriteLine($"CarouselViewController.ScrollToPosition: Skipping scroll due to invalid goToPosition ({goToPosition})");
return;
}

if (ItemsView is not CarouselView carousel)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ internal void UpdateLoop()

void ScrollToPosition(int goToPosition, int carouselPosition, bool animate, bool forceScroll = false)
{
if (goToPosition < 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mirror the same guards in CarouselViewController2 to maintain parity

{
System.Diagnostics.Debug.WriteLine($"CarouselViewController2.ScrollToPosition: Skipping scroll due to invalid goToPosition ({goToPosition}).");
return;
}

if (ItemsView is not CarouselView carousel)
{
return;
Expand Down
68 changes: 68 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue32139.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 32139, "CarouselView scrolls to last item when Loop is enabled and CurrentItem is set to an item not in the list", PlatformAffected.iOS)]
public class Issue32139 : ContentPage
{
const string InitialItem = "0";
const string InvalidItem = "14";

Label selectedItemLabel;
CarouselView carouselView;
public Issue32139()
{
carouselView = new CarouselView
{
HeightRequest = 150,
Loop = true,
ItemsSource = new ObservableCollection<string> { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" },
ItemTemplate = new DataTemplate(() =>
{
Border border = new Border
{
BackgroundColor = Colors.LightGray,
Content = new Label
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
FontSize = 80
}
};
border.Content.SetBinding(Label.TextProperty, ".");
return border;
})
};

selectedItemLabel = new Label
{
Text = $"Selected Item : {InitialItem}",
FontSize = 16
};

Button selectButton = new Button
{
AutomationId = "Issue32139SelectBtn",
Text = $"Select item {InvalidItem}",
FontSize = 12
};

selectButton.Clicked += (s, e) =>
{
carouselView.CurrentItem = InvalidItem;
selectedItemLabel.Text = $"Selected Item : {carouselView.CurrentItem}";
};

Content = new VerticalStackLayout
{
Padding = 25,
Spacing = 15,
Children =
{
carouselView,
selectedItemLabel,
selectButton,
}
};
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if TEST_FAILS_ON_WINDOWS && TEST_FAILS_ON_ANDROID // Related issues have been reported for both Android and Windows : Android (https://github.com/dotnet/maui/issues/29529) and Windows (https://github.com/dotnet/maui/issues/31670)
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue32139 : _IssuesUITest
{
public Issue32139(TestDevice device) : base(device)
{
}

public override string Issue => "CarouselView scrolls to last item when Loop is enabled and CurrentItem is set to an item not in the list";

[Test]
[Category(UITestCategories.CarouselView)]
public void ValidateNoScrollOnInvalidItemWithLoop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can add another test?

After attempting an invalid CurrentItem selection, rotate the device and verify the same item remains visible to catch layout recomputation paths that might re-trigger a scroll.

{
App.WaitForElement("Issue32139SelectBtn");
App.Tap("Issue32139SelectBtn");

VerifyScreenshot();
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.