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

Commit

Permalink
[WPF] Memory leak when you pop a TabbedPage (#3372) fixes #3267
Browse files Browse the repository at this point in the history
* Fix [WPF] Memory leak when you pop a TabbedPage

* Update ViewRenderer.cs

* Update ViewRenderer.cs
  • Loading branch information
mohachouch authored and rmarinho committed Aug 31, 2018
1 parent e805131 commit dab5442
Showing 1 changed file with 17 additions and 9 deletions.
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

0 comments on commit dab5442

Please sign in to comment.