Allow renderers to specify whether native controls should be eagerly disposed (#129)
authorE.Z. Hart <hartez@users.noreply.github.com>
Sun, 24 Apr 2016 06:13:27 +0000 (00:13 -0600)
committerRui Marinho <me@ruimarinho.net>
Sun, 24 Apr 2016 06:13:27 +0000 (02:13 -0400)
Xamarin.Forms.Platform.Android/NativeViewWrapperRenderer.cs
Xamarin.Forms.Platform.Android/ViewRenderer.cs
Xamarin.Forms.Platform.Android/VisualElementRenderer.cs
Xamarin.Forms.Platform.iOS/NativeViewWrapperRenderer.cs
Xamarin.Forms.Platform.iOS/ViewRenderer.cs

index 933f436..49abc02 100644 (file)
@@ -57,5 +57,7 @@ namespace Xamarin.Forms.Platform.Android
                        if (!handled)
                                base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
                }
+
+               protected override bool ManageNativeControlLifetime => false;
        }
 }
\ No newline at end of file
index 0415ce9..143e6a7 100644 (file)
@@ -69,7 +69,7 @@ namespace Xamarin.Forms.Platform.Android
                {
                        if (disposing && !_disposed)
                        {
-                               if (Control != null)
+                               if (Control != null && ManageNativeControlLifetime)
                                {
                                        Control.RemoveFromParent();
                                        Control.Dispose();
index 5abb809..d44714b 100644 (file)
@@ -212,6 +212,12 @@ namespace Xamarin.Forms.Platform.Android
                        Performance.Stop();
                }
 
+               /// <summary>
+               /// Determines whether the native control is disposed of when this renderer is disposed
+               /// Can be overridden in deriving classes 
+               /// </summary>
+               protected virtual bool ManageNativeControlLifetime => true;
+
                protected override void Dispose(bool disposing)
                {
                        if ((_flags & VisualElementRendererFlags.Disposed) != 0)
@@ -244,11 +250,14 @@ namespace Xamarin.Forms.Platform.Android
                                        _gestureListener = null;
                                }
 
-                               int count = ChildCount;
-                               for (var i = 0; i < count; i++)
+                               if (ManageNativeControlLifetime)
                                {
-                                       AView child = GetChildAt(i);
-                                       child.Dispose();
+                                       int count = ChildCount;
+                                       for (var i = 0; i < count; i++)
+                                       {
+                                               AView child = GetChildAt(i);
+                                               child.Dispose();
+                                       }
                                }
 
                                RemoveAllViews();
index 3d3e686..b21c859 100644 (file)
@@ -68,5 +68,10 @@ namespace Xamarin.Forms.Platform.iOS
                        if (e.OldElement == null)
                                SetNativeControl(Element.NativeView);
                }
+
+               /// <summary>
+               /// The native control we're wrapping isn't ours to dispose of
+               /// </summary>
+               protected override bool ManageNativeControlLifetime => false;
        }
 }
\ No newline at end of file
index 5b3be1c..1186fb4 100644 (file)
@@ -41,11 +41,17 @@ namespace Xamarin.Forms.Platform.iOS
                        return Control.SizeThatFits(size);
                }
 
+               /// <summary>
+               /// Determines whether the native control is disposed of when this renderer is disposed
+               /// Can be overridden in deriving classes 
+               /// </summary>
+               protected virtual bool ManageNativeControlLifetime => true;
+
                protected override void Dispose(bool disposing)
                {
                        base.Dispose(disposing);
 
-                       if (disposing && Control != null)
+                       if (disposing && Control != null && ManageNativeControlLifetime)
                        {
                                Control.Dispose();
                                Control = null;