Skip to content
Merged
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 @@ -30,6 +30,7 @@ public partial class CarouselViewHandler : ItemsViewHandler<CarouselView>
bool _isCarouselViewReady;
bool _isInternalPositionUpdate;
int _gotoPosition = -1;
bool _isCollectionChanged;
NotifyCollectionChangedEventHandler _collectionChanged;
readonly WeakNotifyCollectionChangedProxy _proxy = new();

Expand Down Expand Up @@ -534,6 +535,11 @@ void CarouselScrolled(object sender, ItemsViewScrolledEventArgs e)
}

var position = e.CenterItemIndex;
if (_isCollectionChanged && ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepScrollOffset)
Comment thread
Shalini-Ashokan marked this conversation as resolved.
Comment thread
Shalini-Ashokan marked this conversation as resolved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[major] Windows CarouselView lifecycle/correctness_isCollectionChanged is a persistent bool that is set during Add/Remove but is only cleared when a later CarouselScrolled event runs while ItemsUpdatingScrollMode == KeepScrollOffset. If no scroll event is raised immediately after the collection mutation, or if the mutation happens under another scroll mode and the mode is later changed to KeepScrollOffset, the stale flag causes the next real user scroll to be overwritten with ItemsView.Position and swallowed. Scope this state to the specific KeepScrollOffset mutation and clear it deterministically, e.g. only set it when the mode is KeepScrollOffset and reset it on disconnect / after the relevant scroll callback is consumed.

{
position = ItemsView.Position;
_isCollectionChanged = false;
}

if (position == -1)
{
Expand Down Expand Up @@ -573,7 +579,7 @@ void OnCollectionItemsSourceChanged(object sender, NotifyCollectionChangedEventA
{
// Set flag to disable animation during collection changes
_isInternalPositionUpdate = true;

try
{
var carouselPosition = ItemsView.Position;
Expand All @@ -599,6 +605,12 @@ void OnCollectionItemsSourceChanged(object sender, NotifyCollectionChangedEventA
&& currentItemPosition != -1)
{
carouselPosition = currentItemPosition;
_isCollectionChanged = true;
}

if (e.Action == NotifyCollectionChangedAction.Remove)
{
_isCollectionChanged = true;
Comment thread
Shalini-Ashokan marked this conversation as resolved.
}

if (ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepLastItemInView)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29421.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 29421, "KeepScrollOffset Not Working as Expected in CarouselView", PlatformAffected.UWP)]
public class Issue29421 : ContentPage
{
public Issue29421()
{
var verticalStackLayout = new VerticalStackLayout();
var carouselItems = new ObservableCollection<string>
{
"Item 0",
"Item 1",
"Item 2",
"Item 3",
"Item 4",
};

CarouselView carouselView = new CarouselView
{
ItemsSource = carouselItems,
AutomationId = "carouselview",
ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepScrollOffset,
HeightRequest = 300,
ItemTemplate = new DataTemplate(() =>
{
var grid = new Grid
{
Padding = 10
};

var label = new Label
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
FontSize = 18,
};
label.SetBinding(Label.TextProperty, ".");
label.SetBinding(Label.AutomationIdProperty, ".");

grid.Children.Add(label);
return grid;
}),
HorizontalOptions = LayoutOptions.Fill,
};

var indicatorView = new IndicatorView
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};
carouselView.IndicatorView = indicatorView;

var addButton = new Button
{
Text = "Add item",
AutomationId = "AddButton",
Margin = new Thickness(20),
};

addButton.Clicked += (sender, e) =>
{
carouselItems.Insert(0, "NewItem");
};

verticalStackLayout.Children.Add(addButton);
verticalStackLayout.Children.Add(carouselView);
verticalStackLayout.Children.Add(indicatorView);

Content = verticalStackLayout;
}
}
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,23 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue29421 : _IssuesUITest
{
public override string Issue => "KeepScrollOffset Not Working as Expected in CarouselView";

public Issue29421(TestDevice device)
: base(device)
{ }

[Test]
[Category(UITestCategories.CarouselView)]
public void VerifyCarouselViewKeepScrollOffsetAdd()
{
App.WaitForElement("carouselview");
App.Tap("AddButton");
VerifyScreenshot();
Comment thread
Shalini-Ashokan marked this conversation as resolved.
Comment thread
Shalini-Ashokan marked this conversation as resolved.
Comment thread
Shalini-Ashokan marked this conversation as resolved.
Comment thread
Shalini-Ashokan marked this conversation as resolved.
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading