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

Remove children from native layout when disconnecting handler #1066

Merged
merged 3 commits into from
May 22, 2021
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
12 changes: 11 additions & 1 deletion src/Compatibility/Core/src/Android/Cells/ViewCellRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,17 @@ public void Update(ViewCell cell)
RemoveView(_view.View);
AppCompat.Platform.SetRenderer(_viewCell.View, null);
_viewCell.View.IsPlatformEnabled = false;
_view.View.Dispose();

// Adding a special case for HandlerToRendererShim so that DisconnectHandler gets called;
// Pending https://github.com/xamarin/Xamarin.Forms/pull/14288 being merged, we won't need the special case
if (_view is HandlerToRendererShim htrs)
{
htrs.Dispose();
}
else
{
_view.View.Dispose();
}

_viewCell = cell;
_view = AppCompat.Platform.CreateRenderer(_viewCell.View, Context);
Expand Down
11 changes: 8 additions & 3 deletions src/Core/src/Handlers/Layout/LayoutHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ public override void SetVirtualView(IView view)
NativeView.CrossPlatformMeasure = VirtualView.Measure;
NativeView.CrossPlatformArrange = VirtualView.Arrange;

this.NativeView.RemoveAllViews();
NativeView.RemoveAllViews();
foreach (var child in VirtualView.Children)
{
//var wrap = ViewGroup.LayoutParams.WrapContent;
//NativeView.AddView(child.ToNative(MauiContext), new ViewGroup.LayoutParams(wrap, wrap));
NativeView.AddView(child.ToNative(MauiContext));
}
}
Expand All @@ -60,5 +58,12 @@ public void Remove(IView child)
NativeView.RemoveView(view);
}
}

protected override void DisconnectHandler(LayoutViewGroup nativeView)
{
// If we're being disconnected from the xplat element, then we should no longer be managing its chidren
NativeView?.RemoveAllViews();
base.DisconnectHandler(nativeView);
}
}
}
7 changes: 7 additions & 0 deletions src/Core/src/Handlers/Layout/LayoutHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,12 @@ protected override LayoutPanel CreateNativeView()

return view;
}

protected override void DisconnectHandler(LayoutPanel nativeView)
{
// If we're being disconnected from the xplat element, then we should no longer be managing its chidren
NativeView?.Children.Clear();
base.DisconnectHandler(nativeView);
}
}
}
11 changes: 11 additions & 0 deletions src/Core/src/Handlers/Layout/LayoutHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,16 @@ public void Remove(IView child)
NativeView.SetNeedsLayout();
}
}

protected override void DisconnectHandler(LayoutView nativeView)
{
base.DisconnectHandler(nativeView);
var subViews = nativeView.Subviews;

foreach (var subView in subViews)
{
subView.RemoveFromSuperview();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,23 @@ public async Task HandlerRemovesChildFromNativeLayout()

Assert.Equal(0, count);
}

[Fact(DisplayName = "DisconnectHandler removes child from native layout")]
public async Task DisconnectHandlerRemovesChildFromNativeLayout()
{
var layout = new LayoutStub();
var slider = new SliderStub();
layout.Add(slider);

var handler = await CreateHandlerAsync(layout);

var count = await InvokeOnMainThreadAsync(() =>
{
layout.Handler.DisconnectHandler();
return GetNativeChildCount(handler);
});

Assert.Equal(0, count);
}
}
}