-
Notifications
You must be signed in to change notification settings - Fork 2k
Fixed [Windows] KeepScrollOffset Behavior Not Working as Expected in CarouselView #29800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
|
|
||
|
|
@@ -534,6 +535,11 @@ void CarouselScrolled(object sender, ItemsViewScrolledEventArgs e) | |
| } | ||
|
|
||
| var position = e.CenterItemIndex; | ||
| if (_isCollectionChanged && ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepScrollOffset) | ||
|
Shalini-Ashokan marked this conversation as resolved.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [major] Windows CarouselView lifecycle/correctness — |
||
| { | ||
| position = ItemsView.Position; | ||
| _isCollectionChanged = false; | ||
| } | ||
|
|
||
| if (position == -1) | ||
| { | ||
|
|
@@ -573,7 +579,7 @@ void OnCollectionItemsSourceChanged(object sender, NotifyCollectionChangedEventA | |
| { | ||
| // Set flag to disable animation during collection changes | ||
| _isInternalPositionUpdate = true; | ||
|
|
||
| try | ||
| { | ||
| var carouselPosition = ItemsView.Position; | ||
|
|
@@ -599,6 +605,12 @@ void OnCollectionItemsSourceChanged(object sender, NotifyCollectionChangedEventA | |
| && currentItemPosition != -1) | ||
| { | ||
| carouselPosition = currentItemPosition; | ||
| _isCollectionChanged = true; | ||
| } | ||
|
|
||
| if (e.Action == NotifyCollectionChangedAction.Remove) | ||
| { | ||
| _isCollectionChanged = true; | ||
|
Shalini-Ashokan marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (ItemsView.ItemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepLastItemInView) | ||
|
|
||
| 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; | ||
| } | ||
| } |
| 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(); | ||
|
Shalini-Ashokan marked this conversation as resolved.
Shalini-Ashokan marked this conversation as resolved.
Shalini-Ashokan marked this conversation as resolved.
Shalini-Ashokan marked this conversation as resolved.
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.