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 @@ -523,6 +523,11 @@ void CleanUpCollectionViewSource()
if (itemContainer?.Child is ElementWrapper wrapper && wrapper.VirtualView is View mauiView)
{
mauiView.DisconnectHandlers();
if (mauiView.Handler is null && wrapper.Content is not null)
{
wrapper.UnwireContentSizeObserver();
wrapper.Content = null;
}
VirtualView.RemoveLogicalChild(mauiView);
}
}
Expand Down
35 changes: 27 additions & 8 deletions src/Controls/src/Core/Handlers/Items2/Windows/ItemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ internal partial class ElementWrapper : ContentControl

// Stored so RecycleElement can unsubscribe without a flag — mirrors iOS prepareForReuse.
SizeChangedEventHandler? _contentSizeChangedHandler;
RoutedEventHandler? _contentUnloadedHandler;
FrameworkElement? _observedContent;
WAccessibilityView? _defaultAccessibilityView;
bool _semanticsRefreshAttached;
Expand All @@ -369,17 +370,27 @@ internal bool TryAttachSemanticsRefresh()
}

/// <summary>
/// Unsubscribes the first-item SizeChanged observer wired during MeasureOverride.
/// Unsubscribes the first-item SizeChanged and Unloaded observers wired during MeasureOverride.
/// Must be called from RecycleElement so a recycled wrapper cannot poison the global size cache.
/// </summary>
internal void UnwireContentSizeObserver()
{
if (_observedContent != null && _contentSizeChangedHandler != null)
if (_observedContent is not null)
{
_observedContent.SizeChanged -= _contentSizeChangedHandler;
_contentSizeChangedHandler = null;
_observedContent = null;
if (_contentSizeChangedHandler is not null)
{
_observedContent.SizeChanged -= _contentSizeChangedHandler;
}

if (_contentUnloadedHandler is not null)
{
_observedContent.Unloaded -= _contentUnloadedHandler;
}
}

_contentSizeChangedHandler = null;
_contentUnloadedHandler = null;
_observedContent = null;
}

public ElementWrapper(IMauiContext context)
Expand Down Expand Up @@ -518,11 +529,19 @@ void OnContentUnloaded(object? sender, RoutedEventArgs e)
element.SizeChanged -= OnContentSizeChanged;
element.Unloaded -= OnContentUnloaded;
}
_contentSizeChangedHandler = null;
_observedContent = null;

// A late unload from replaced content must not clear the current observer.
if (ReferenceEquals(_observedContent, sender))
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved

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.

[moderate] Regression Prevention and Test Coverage - No discriminating test protects this stale-sender guard or the replacement observer's lifecycle. Issue40414 uses MeasureAllItems and static labels, so its screenshot cannot verify that replacement size growth updates the MeasureFirstItem cache. Existing reattachment and source-swap tests also do not deliver an old content element's Unloaded callback after installing a replacement observer. Add a focused Windows test for that ordering, assert that the replacement remains tracked and updates the cache on growth, then recycle it and assert that subsequent growth cannot update the cache. Removing the guard should fail that test; the current navigation test does not distinguish the guarded and unguarded implementations.

{
_contentSizeChangedHandler = null;
_contentUnloadedHandler = null;
_observedContent = null;
}
}
// Store delegate and content so RecycleElement can unsubscribe directly.
UnwireContentSizeObserver();
// Store delegates and content so RecycleElement can unsubscribe directly.
_contentSizeChangedHandler = OnContentSizeChanged;
_contentUnloadedHandler = OnContentUnloaded;
_observedContent = content;
content.SizeChanged += OnContentSizeChanged;
content.Unloaded += OnContentUnloaded;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue40414.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Collections.Generic;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 40414, "CollectionViewHandler2 does not display grouped CollectionView items after ItemsSource is reset", PlatformAffected.UWP)]
public class Issue40414 : Shell
{
public Issue40414()
{
FlyoutBehavior = FlyoutBehavior.Flyout;

Items.Add(new ShellContent
{
Title = "Flyout1Content",
ContentTemplate = new DataTemplate(() => new ContentPage
{
Content = new Label
{
Text = "Open Flyout2Content, return to Flyout1Content, then open Flyout2Content again. The grouped items should be visible.",
AutomationId = "Flyout1Label"
}
})
});

Items.Add(new ShellContent
{
Title = "Flyout2Content",
ContentTemplate = new DataTemplate(() => new Issue40414CollectionPage())
});
}
}

class Issue40414CollectionPage : ContentPage
{
readonly CollectionView _collectionView;

public Issue40414CollectionPage()
{
_collectionView = new CollectionView
{
IsGrouped = true,
ItemSizingStrategy = ItemSizingStrategy.MeasureAllItems,
SelectionMode = SelectionMode.None,
AutomationId = "GroupedCollectionView",
ItemsLayout = new GridItemsLayout(5, ItemsLayoutOrientation.Vertical),
// A layout as the template root is required to reproduce the issue.
ItemTemplate = new DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, ".");
return new HorizontalStackLayout { Children = { label } };
}),
GroupHeaderTemplate = new DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, nameof(Issue40414Group.GroupName));
return new HorizontalStackLayout { Children = { new Label { Text = "Group: " }, label } };
})
};

Padding = 16;
Content = _collectionView;
}

protected override void OnAppearing()
{
base.OnAppearing();

_collectionView.ItemsSource = new List<Issue40414Group>
{
new Issue40414Group("1", ["A", "B", "C", "D", "E", "F", "G"]),
new Issue40414Group("2", ["A", "B", "C", "D", "E", "F", "G"]),
new Issue40414Group("3", ["A", "B", "C", "D", "E", "F", "G"]),
};
}
}

public class Issue40414Group : List<string>
{
public Issue40414Group(string groupName, List<string> items) : base(items)
{
GroupName = groupName;
}

public string GroupName { get; }
}
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,32 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue40414 : _IssuesUITest
{
public override string Issue => "CollectionViewHandler2 does not display grouped CollectionView items after ItemsSource is reset";

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

[Test]
[ShardedTestCategory(UITestCategories.CollectionView)]
public void GroupedItemsAreDisplayedAfterNavigatingBackToFlyoutPage()
{
App.WaitForElement("Flyout1Label");

App.TapInShellFlyout("Flyout2Content");
App.WaitForElement("GroupedCollectionView");

App.TapInShellFlyout("Flyout1Content");
App.WaitForElement("Flyout1Label");

App.TapInShellFlyout("Flyout2Content");
App.WaitForElement("GroupedCollectionView");

VerifyScreenshot(retryTimeout: TimeSpan.FromSeconds(2));
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved

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.

[moderate] Regression Prevention and Test Coverage - The added baselines still omit two enabled configurations: snapshots/windows-cv1 and snapshots/ios. The CollectionView shard runs in the Windows CV1 lane with UseWindowsCV1:true and in the iOS 18.4 lane (eng/pipelines/common/ui-tests.yml and eng/pipelines/ui-tests.yml). VerifyScreenshot() selects windows-cv1 and ios respectively, and VisualRegressionTester requires that exact directory without falling back to windows or ios-26. These configurations therefore fail with a missing baseline even when the grouped items render correctly. Add GroupedItemsAreDisplayedAfterNavigatingBackToFlyoutPage.png for both configurations, or replace the screenshot assertion with rendered-content assertions.

}
}
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