Skip to content
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

[Core] Changes disposing items in CollectionView #10590

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions src/Controls/src/Core/Items/ItemsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,32 @@ public void RemoveLogicalChild(Element element)
_logicalChildren.Remove(element);
OnChildRemoved(element, oldLogicalIndex);
VisualDiagnostics.OnChildRemoved(this, element, oldLogicalIndex);
DisconnectLogicalChild(element);
}


void DisconnectLogicalChild(Element element)
{
#if ANDROID || IOS || MACCATALYST || WINDOWS
if (element.Handler is IPlatformViewHandler platformHandler)
{
foreach (Element child in element.Descendants())
{
if (child is VisualElement ve)
{
ve.Handler?.DisconnectHandler();

if (ve.Handler is IDisposable veHandlerDisposable)
veHandlerDisposable.Dispose();
Copy link
Member

Choose a reason for hiding this comment

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

If you remove all the dispose code does that still work?

I don't think we want to start pulling dispose patterns in the code here.

}
}

platformHandler.DisconnectHandler();

if (platformHandler is IDisposable platformHandlerDisposable)
platformHandlerDisposable.Dispose();
}
#endif
}

internal override IReadOnlyList<Element> LogicalChildrenInternal => _logicalChildren.AsReadOnly();
Expand Down
3 changes: 3 additions & 0 deletions src/Core/src/Handlers/View/ViewHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ partial void DisconnectingHandler(PlatformView platformView)

platformView.GotFocus -= OnPlatformViewGotFocus;
platformView.LostFocus -= OnPlatformViewLostFocus;

if (platformView is IDisposable disposable)
disposable.Dispose();
}

static partial void MappingFrame(IViewHandler handler, IView view)
Expand Down
4 changes: 1 addition & 3 deletions src/Core/src/Platform/Android/LayoutViewGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Microsoft.Maui.Graphics;
using ARect = Android.Graphics.Rect;
using Rectangle = Microsoft.Maui.Graphics.Rect;
using Size = Microsoft.Maui.Graphics.Size;

namespace Microsoft.Maui.Platform
{
public class LayoutViewGroup : ViewGroup
public class LayoutViewGroup : ViewGroup, IDisposable
{
readonly ARect _clipRect = new();
readonly Context _context;
Expand Down
7 changes: 6 additions & 1 deletion src/Core/src/Platform/Windows/LayoutPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Microsoft.Maui.Platform
{
public class LayoutPanel : Panel
public class LayoutPanel : Panel, IDisposable
{
Canvas? _backgroundLayer;

Expand All @@ -18,6 +18,11 @@ public class LayoutPanel : Panel

public bool ClipsToBounds { get; set; }

public void Dispose()
{
Children.Clear();
}

// TODO: Possibly reconcile this code with ViewHandlerExtensions.MeasureVirtualView
// If you make changes here please review if those changes should also
// apply to ViewHandlerExtensions.MeasureVirtualView
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Platform/iOS/LayoutView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.Maui.Platform
{
public class LayoutView : MauiView
public class LayoutView : MauiView, IDisposable
{
bool _userInteractionEnabled;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Microsoft.Maui.IMenuFlyoutSeparator
Microsoft.Maui.IToolTipElement
Microsoft.Maui.IToolTipElement.ToolTip.get -> Microsoft.Maui.ToolTip?
Microsoft.Maui.IWindow.FrameChanged(Microsoft.Maui.Graphics.Rect frame) -> void
Microsoft.Maui.Platform.LayoutPanel.Dispose() -> void
Microsoft.Maui.ToolTip
Microsoft.Maui.ToolTip.Content.get -> object?
Microsoft.Maui.ToolTip.Content.set -> void
Expand Down