From 2747cdc7a6b34dd336d1a017f4653a6b43c58699 Mon Sep 17 00:00:00 2001 From: Samantha Houts Date: Tue, 15 Aug 2017 11:18:57 -0700 Subject: [Android] Eagerly dispose children of ListViews to prevent ObjectDisposed exception (#1063) * Add repro for 57910 * [Android] Dispose cells explicitly * [Android] Add default constructors to prevent crash on dispose * [Android] Don't try to dispose headers and footers --- .../CustomRenderers.cs | 55 +++++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'Xamarin.Forms.ControlGallery.Android') diff --git a/Xamarin.Forms.ControlGallery.Android/CustomRenderers.cs b/Xamarin.Forms.ControlGallery.Android/CustomRenderers.cs index 33310332..e219b82c 100644 --- a/Xamarin.Forms.ControlGallery.Android/CustomRenderers.cs +++ b/Xamarin.Forms.ControlGallery.Android/CustomRenderers.cs @@ -31,6 +31,8 @@ using Xamarin.Forms.Controls.Issues; [assembly: ExportRenderer(typeof(Bugzilla42000._42000NumericEntryNoDecimal), typeof(EntryRendererNoDecimal))] [assembly: ExportRenderer(typeof(Bugzilla42000._42000NumericEntryNoNegative), typeof(EntryRendererNoNegative))] //[assembly: ExportRenderer(typeof(AndroidHelpText.HintLabel), typeof(HintLabel))] +[assembly: ExportRenderer(typeof(Bugzilla57910QuickCollectNavigationPage), typeof(QuickCollectNavigationPage))] + [assembly: ExportRenderer(typeof(Xamarin.Forms.Controls.Issues.NoFlashTestNavigationPage), typeof(Xamarin.Forms.ControlGallery.Android.NoFlashTestNavigationPage))] @@ -523,7 +525,6 @@ namespace Xamarin.Forms.ControlGallery.Android } } - //public class HintLabel : Xamarin.Forms.Platform.Android.AppCompat.LabelRenderer //{ // public HintLabel() @@ -531,7 +532,7 @@ namespace Xamarin.Forms.ControlGallery.Android // Hint = AndroidHelpText.HintLabel.Success; // } // } - + public class NoFlashTestNavigationPage : Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer { protected override void SetupPageTransition(global::Android.Support.V4.App.FragmentTransaction transaction, bool isPush) @@ -539,5 +540,55 @@ namespace Xamarin.Forms.ControlGallery.Android transaction.SetTransition((int)FragmentTransit.None); } } + + public class QuickCollectNavigationPage : Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer + { + bool _disposed; + NavigationPage _page; + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + base.OnElementChanged(e); + + if (e.NewElement == null) + { + if (e.OldElement != null) + { + ((IPageController)e.OldElement).InternalChildren.CollectionChanged -= OnInternalPageCollectionChanged; + } + + return; + } + + ((IPageController)e.NewElement).InternalChildren.CollectionChanged += OnInternalPageCollectionChanged; + } + + private void OnInternalPageCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + { + if (e.OldItems != null) + { + // Force a collection on popped to simulate the problem. + GC.Collect(); + } + } + + protected override void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + _disposed = true; + + if (disposing && _page != null) + { + _page.InternalChildren.CollectionChanged -= OnInternalPageCollectionChanged; + _page = null; + } + + base.Dispose(disposing); + } + } } -- cgit v1.2.3