Skip to content

[Windows] Fixed KeepLastItemInView Behavior Not Working as Expected in CarouselView#29550

Open
NanthiniMahalingam wants to merge 14 commits intodotnet:inflight/currentfrom
NanthiniMahalingam:fix-29420
Open

[Windows] Fixed KeepLastItemInView Behavior Not Working as Expected in CarouselView#29550
NanthiniMahalingam wants to merge 14 commits intodotnet:inflight/currentfrom
NanthiniMahalingam:fix-29420

Conversation

@NanthiniMahalingam
Copy link
Copy Markdown
Contributor

@NanthiniMahalingam NanthiniMahalingam commented May 16, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Root Cause

  • The CarouselView does not correctly identify the last item when KeepLastInView is enabled due to the presence of fake (looping) items.
  • In the OnItemsVectorChanged method, the item count returned includes these fake items, which prevents the actual last item from being recognized.
  • When dynamically resizing the window, the ItemsView position changes based on the CenterVisibleIndex value within the CarouselScrolled method. As a result, the current item scrolls away from the last item unexpectedly.

Description of Change

  • Updated the OnItemsVectorChanged method to use the CollectionViewSource.View of ItemCount to retrieve the actual items of the CarouselView, avoiding fake items.
  • This ensures that the correct last item is identified and displayed when KeepLastInView is set.
  • Restricted the ItemsView.ScrollTo call in UpdateCurrentItem method when ItemsView.Position is equal to the current item’s position due to items view position changed based on CenterVisibleIndex value inside the CarosuelScrolled method which caused that the current items has been scrolled from the last item while dynamically resizing the window.

Validated the behaviour in the following platforms

Issues Fixed

Fixes #29420
Fixes #29448

Output

Windows

Before After
Issue29420_Before.mp4
Issue29420_After.mp4

@dotnet-policy-service dotnet-policy-service bot added community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration labels May 16, 2025
@jsuarezruiz jsuarezruiz added platform/windows area-controls-collectionview CollectionView, CarouselView, IndicatorView labels May 19, 2025
@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@NanthiniMahalingam NanthiniMahalingam marked this pull request as ready for review May 19, 2025 15:24
Copilot AI review requested due to automatic review settings May 19, 2025 15:24
@NanthiniMahalingam NanthiniMahalingam requested a review from a team as a code owner May 19, 2025 15:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the KeepLastItemInView behavior in CarouselView on Windows by adjusting how the last item is identified and preventing unwanted scroll operations when the carousel is resized.

  • Updated the item count calculation in OnItemsVectorChanged to avoid using fake looping items.
  • Restricted the ScrollTo call in UpdateCurrentItem when the current item is already in view.
  • Added test cases in both shared tests and the host app to validate the behavior on Windows (and other platforms).

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29420.cs Added test case for KeepLastItemInView on platforms failing the behavior
src/Controls/tests/TestCases.HostApp/Issues/Issue29420.cs Host test implementation verifying the fixed behavior
src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Windows.cs Modified item count logic to differentiate between fake looping items and actual items
src/Controls/src/Core/Handlers/Items/CarouselViewHandler.Windows.cs Added a safeguard in UpdateCurrentItem to avoid unnecessary scroll operations

return;

var itemsCount = items.Count;
// When looping is enabled in CarouselView, Items.Count returns the FakeCount instead of the actual item count.
Copy link

Copilot AI May 19, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding an inline comment clarifying why VirtualView is being checked against CollectionView and what types are expected; this will help future maintainers understand the intended behavior when selecting between items.Count and ItemCount.

Suggested change
// When looping is enabled in CarouselView, Items.Count returns the FakeCount instead of the actual item count.
// When looping is enabled in CarouselView, Items.Count returns the FakeCount instead of the actual item count.
// Use items.Count for CollectionView to account for this behavior; otherwise, use ItemCount for other types.

Copilot uses AI. Check for mistakes.
@@ -381,8 +381,10 @@ void UpdateCurrentItem()

var currentItemPosition = GetItemPositionInCarousel(ItemsView.CurrentItem);

Copy link

Copilot AI May 19, 2025

Choose a reason for hiding this comment

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

[nitpick] It would be helpful to add a brief comment explaining the rationale for including the check against ItemsView.Position, so that the intention to prevent unnecessary scrolling when the current item is already visible is clearly documented.

Suggested change
// If the current item is already visible (ItemsView.Position == currentItemPosition),
// avoid unnecessary scrolling to improve performance and user experience.

Copilot uses AI. Check for mistakes.
@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

jsuarezruiz
jsuarezruiz previously approved these changes Jun 17, 2025
var currentItemPosition = GetItemPositionInCarousel(ItemsView.CurrentItem);

if (currentItemPosition < 0 || currentItemPosition >= ItemCount)
if (currentItemPosition < 0 || currentItemPosition >= ItemCount || ItemsView.Position == currentItemPosition)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not an extra long condition, but can increase the readability here adding a couple of variables:

bool isOutOfBounds = currentItemPosition < 0 || currentItemPosition >= ItemCount;
bool isSamePosition = ItemsView.Position == currentItemPosition;

if (isOutOfBounds || isSamePosition)
    return;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not an extra long condition, but can increase the readability here adding a couple of variables:

bool isOutOfBounds = currentItemPosition < 0 || currentItemPosition >= ItemCount;
bool isSamePosition = ItemsView.Position == currentItemPosition;

if (isOutOfBounds || isSamePosition)
    return;

Hi @jsuarezruiz ,
I've refined the fix according to your suggestion to enhance readability.

@NanthiniMahalingam NanthiniMahalingam changed the title Fixed [Windows] KeepLastItemInView Behavior Not Working as Expected in CarouselView [Windows] Fixed KeepLastItemInView Behavior Not Working as Expected in CarouselView Jun 19, 2025
@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 9, 2025

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 29550

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 29550"

@MauiBot MauiBot added s/agent-approved AI agent recommends approval - PR fix is correct and optimal s/agent-fix-pr-picked AI could not beat the PR fix - PR is the best among all candidates and removed s/agent-review-incomplete AI agent could not complete all phases (blocker, timeout, error) s/agent-fix-win AI found a better alternative fix than the PR labels Mar 26, 2026
@kubaflo
Copy link
Copy Markdown
Contributor

kubaflo commented Mar 26, 2026

/azp run maui-pr-uitests , maui-pr-devicetests

@kubaflo kubaflo changed the base branch from main to inflight/current March 26, 2026 20:11
Copy link
Copy Markdown
Contributor

@kubaflo kubaflo left a comment

Choose a reason for hiding this comment

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

Could you please resolve conflicts?

@SubhikshaSf4851
Copy link
Copy Markdown
Contributor

@kubaflo I have resolved the conflicts

@MauiBot
Copy link
Copy Markdown
Collaborator

MauiBot commented Mar 27, 2026

🚦 Gate - Test Before and After Fix

📊 Expand Full Gatef4edce5 · Added missed logic on resolving conflict

Gate Result: ❌ FAILED

Platform: windows


@MauiBot MauiBot added s/agent-changes-requested AI agent recommends changes - found a better alternative or issues s/agent-fix-win AI found a better alternative fix than the PR and removed s/agent-approved AI agent recommends approval - PR fix is correct and optimal s/agent-fix-pr-picked AI could not beat the PR fix - PR is the best among all candidates labels Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/windows s/agent-changes-requested AI agent recommends changes - found a better alternative or issues s/agent-fix-win AI found a better alternative fix than the PR s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Windows] CarouselView ScrollTo Method Fails to Scroll to Index [Windows] KeepLastItemInView Behavior Not Working as Expected in CarouselView

9 participants