Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

[WPF] Memory leak when you pop a TabbedPage #3372

Merged
merged 3 commits into from
Aug 31, 2018
Merged
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
26 changes: 17 additions & 9 deletions Xamarin.Forms.Platform.WPF/Renderers/ViewRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using WControl = System.Windows.Controls.Control;
Expand All @@ -23,6 +24,7 @@ public class ViewRenderer<TElement, TNativeElement> : IVisualElementRenderer, IE
new List<EventHandler<VisualElementChangedEventArgs>>();

VisualElementTracker _tracker;
bool _disposed;

IElementController ElementController => Element as IElementController;

Expand Down Expand Up @@ -161,19 +163,28 @@ protected void SetNativeControl(TNativeElement native)

Element.IsNativeStateConsistent = false;

Control.Loaded += (sender, e) =>
{
Element.IsNativeStateConsistent = true;
Appearing();
};
Control.Unloaded += (sender, e) => { Disappearing(); };
Control.Loaded += Control_Loaded;
Control.Unloaded += Control_Unloaded;

Control.GotFocus += OnGotFocus;
Control.LostFocus += OnLostFocus;

UpdateBackground();
UpdateAlignment();
}

private void Control_Loaded(object sender, RoutedEventArgs e)
{
Control.Loaded -= Control_Loaded;
Element.IsNativeStateConsistent = true;
Appearing();
}

private void Control_Unloaded(object sender, RoutedEventArgs e)
{
Control.Unloaded -= Control_Unloaded;
Disappearing();
}

protected virtual void Appearing()
{
Expand Down Expand Up @@ -260,8 +271,6 @@ void UpdateAlignment()
}
}

bool _disposed;

public void Dispose()
{
Dispose(true);
Expand All @@ -276,7 +285,6 @@ protected virtual void Dispose(bool disposing)

if (Control != null)
{
//Console.WriteLine("Dispose : " + this.Control.GetType());
Control.GotFocus -= OnGotFocus;
Control.LostFocus -= OnLostFocus;
}
Expand Down