Skip to content

[.NET10] Made MauiCollectionView on iOS public #29917

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

Merged
merged 3 commits into from
Jun 20, 2025
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
30 changes: 16 additions & 14 deletions src/Controls/src/Core/Handlers/Items/iOS/ItemsViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Microsoft.Maui.Controls.Handlers.Items
{
public abstract class ItemsViewController<TItemsView> : UICollectionViewController, MauiCollectionView.ICustomMauiCollectionViewDelegate
public abstract class ItemsViewController<TItemsView> : UICollectionViewController
where TItemsView : ItemsView
{
public const int EmptyTag = 333;
Expand Down Expand Up @@ -89,6 +89,8 @@ protected override void Dispose(bool disposing)
if (disposing)
{
ItemsSource?.Dispose();

((IUIViewLifeCycleEvents)CollectionView).MovedToWindow -= MovedToWindow;

CollectionView.Delegate = null;
Delegator?.Dispose();
Expand Down Expand Up @@ -187,10 +189,22 @@ public override void LoadView()
{
base.LoadView();
var collectionView = new MauiCollectionView(CGRect.Empty, ItemsViewLayout);
collectionView.SetCustomDelegate(this);
((IUIViewLifeCycleEvents)collectionView).MovedToWindow += MovedToWindow;
CollectionView = collectionView;
}

private void MovedToWindow(object sender, EventArgs e)
{
if (CollectionView?.Window != null)
{
AttachingToWindow();
}
else
{
DetachingFromWindow();
}
}

public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
Expand Down Expand Up @@ -275,18 +289,6 @@ bool IsRefreshing()
return false;
}

void MauiCollectionView.ICustomMauiCollectionViewDelegate.MovedToWindow(UIView view)
{
if (CollectionView?.Window != null)
{
AttachingToWindow();
}
else
{
DetachingFromWindow();
}
}

void InvalidateMeasureIfContentSizeChanged()
{
var contentSize = CollectionView?.CollectionViewLayout?.CollectionViewContentSize;
Expand Down
28 changes: 5 additions & 23 deletions src/Controls/src/Core/Handlers/Items/iOS/MauiCollectionView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

namespace Microsoft.Maui.Controls.Handlers.Items;

internal class MauiCollectionView : UICollectionView, IUIViewLifeCycleEvents, IPlatformMeasureInvalidationController
public class MauiCollectionView : UICollectionView, IUIViewLifeCycleEvents, IPlatformMeasureInvalidationController
{
bool _invalidateParentWhenMovedToWindow;

WeakReference<ICustomMauiCollectionViewDelegate>? _customDelegate;
readonly WeakEventManager _movedToWindowEventManager = new();

internal bool NeedsCellLayout { get; set; }

Expand Down Expand Up @@ -40,39 +40,21 @@ bool IPlatformMeasureInvalidationController.InvalidateMeasure(bool isPropagating
}

[UnconditionalSuppressMessage("Memory", "MEM0002", Justification = IUIViewLifeCycleEvents.UnconditionalSuppressMessage)]
EventHandler? _movedToWindow;

event EventHandler? IUIViewLifeCycleEvents.MovedToWindow
{
add => _movedToWindow += value;
remove => _movedToWindow -= value;
add => _movedToWindowEventManager.AddEventHandler(value);
remove => _movedToWindowEventManager.RemoveEventHandler(value);
}

public override void MovedToWindow()
{
base.MovedToWindow();
_movedToWindow?.Invoke(this, EventArgs.Empty);

if (_customDelegate?.TryGetTarget(out var target) == true)
{
target.MovedToWindow(this);
}
_movedToWindowEventManager.HandleEvent(this, EventArgs.Empty, nameof(IUIViewLifeCycleEvents.MovedToWindow));

if (_invalidateParentWhenMovedToWindow)
{
_invalidateParentWhenMovedToWindow = false;
this.InvalidateAncestorsMeasures();
}
}

internal void SetCustomDelegate(ICustomMauiCollectionViewDelegate customDelegate)
{
_customDelegate = new WeakReference<ICustomMauiCollectionViewDelegate>(customDelegate);
}


internal interface ICustomMauiCollectionViewDelegate
{
void MovedToWindow(UIView view);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Microsoft.Maui.Controls.Handlers.Items2
{
public abstract class ItemsViewController2<TItemsView> : UICollectionViewController, Items.MauiCollectionView.ICustomMauiCollectionViewDelegate
public abstract class ItemsViewController2<TItemsView> : UICollectionViewController
where TItemsView : ItemsView
{
public const int EmptyTag = 333;
Expand Down Expand Up @@ -90,6 +90,8 @@ protected override void Dispose(bool disposing)
{
ItemsSource?.Dispose();

((IUIViewLifeCycleEvents)CollectionView).MovedToWindow -= MovedToWindow;

CollectionView.Delegate = null;
Delegator?.Dispose();

Expand Down Expand Up @@ -186,7 +188,7 @@ public override void LoadView()
{
base.LoadView();
var collectionView = new Items.MauiCollectionView(CGRect.Empty, ItemsViewLayout);
collectionView.SetCustomDelegate(this);
((IUIViewLifeCycleEvents)collectionView).MovedToWindow += MovedToWindow;
CollectionView = collectionView;
}

Expand Down Expand Up @@ -228,7 +230,7 @@ void InvalidateLayoutIfItemsMeasureChanged()
}
}

void Items.MauiCollectionView.ICustomMauiCollectionViewDelegate.MovedToWindow(UIView view)
private void MovedToWindow(object sender, EventArgs e)
{
if (CollectionView?.Window != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#nullable enable
Microsoft.Maui.Controls.Handlers.Items.MauiCollectionView
Microsoft.Maui.Controls.Handlers.Items.MauiCollectionView.MauiCollectionView(CoreGraphics.CGRect frame, UIKit.UICollectionViewLayout! layout) -> void
override Microsoft.Maui.Controls.Handlers.Items.MauiCollectionView.MovedToWindow() -> void
override Microsoft.Maui.Controls.Handlers.Items.MauiCollectionView.ScrollRectToVisible(CoreGraphics.CGRect rect, bool animated) -> void
Microsoft.Maui.Controls.LayoutConstraint
Microsoft.Maui.Controls.LayoutConstraint.Fixed = Microsoft.Maui.Controls.LayoutConstraint.HorizontallyFixed | Microsoft.Maui.Controls.LayoutConstraint.VerticallyFixed -> Microsoft.Maui.Controls.LayoutConstraint
Microsoft.Maui.Controls.LayoutConstraint.HorizontallyFixed = 1 -> Microsoft.Maui.Controls.LayoutConstraint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#nullable enable
Microsoft.Maui.Controls.Handlers.Items.MauiCollectionView
Microsoft.Maui.Controls.Handlers.Items.MauiCollectionView.MauiCollectionView(CoreGraphics.CGRect frame, UIKit.UICollectionViewLayout! layout) -> void
override Microsoft.Maui.Controls.Handlers.Items.MauiCollectionView.MovedToWindow() -> void
override Microsoft.Maui.Controls.Handlers.Items.MauiCollectionView.ScrollRectToVisible(CoreGraphics.CGRect rect, bool animated) -> void
Microsoft.Maui.Controls.LayoutConstraint
Microsoft.Maui.Controls.LayoutConstraint.Fixed = Microsoft.Maui.Controls.LayoutConstraint.HorizontallyFixed | Microsoft.Maui.Controls.LayoutConstraint.VerticallyFixed -> Microsoft.Maui.Controls.LayoutConstraint
Microsoft.Maui.Controls.LayoutConstraint.HorizontallyFixed = 1 -> Microsoft.Maui.Controls.LayoutConstraint
Expand Down
Loading