From 4a51859da1d76580473803c44b9864844c875dc9 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Fri, 10 Nov 2017 10:14:28 -0800 Subject: [PATCH] Switch to reusing renderers on Forms elements --- Ooui.Forms/PageExtensions.cs | 12 ++++++++++-- Samples/BoxViewClockSample.cs | 10 +++++++++- Samples/XamarinFormsSample.cs | 4 ++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Ooui.Forms/PageExtensions.cs b/Ooui.Forms/PageExtensions.cs index 8f60bd1..4ffe01d 100644 --- a/Ooui.Forms/PageExtensions.cs +++ b/Ooui.Forms/PageExtensions.cs @@ -16,16 +16,24 @@ namespace Xamarin.Forms Ooui.UI.Publish (path, () => lazyPage.Value); } - public static Ooui.Element CreateElement (this Xamarin.Forms.Page page) + public static Ooui.Element GetOouiElement (this Xamarin.Forms.Page page) { if (!Xamarin.Forms.Forms.IsInitialized) throw new InvalidOperationException ("call Forms.Init() before this"); + + var existingRenderer = Ooui.Forms.Platform.GetRenderer (page); + if (existingRenderer != null) + return existingRenderer.NativeView; + return CreateElement (page); + } + + static Ooui.Element CreateElement (this Xamarin.Forms.Page page) + { if (!(page.RealParent is Application)) { var app = new DefaultApplication (); app.MainPage = page; } - var result = new Ooui.Forms.Platform (); result.SetPage (page); return result.Element; diff --git a/Samples/BoxViewClockSample.cs b/Samples/BoxViewClockSample.cs index 2fb684b..f576061 100644 --- a/Samples/BoxViewClockSample.cs +++ b/Samples/BoxViewClockSample.cs @@ -7,9 +7,17 @@ namespace Samples { public string Title => "Xamarin.Forms BoxViewClock"; + BoxViewClockPage page; + public Ooui.Element CreateElement () { - return new BoxViewClockPage ().CreateElement (); + // + // Always return the same page because the code never stops the timer + // and we don't want to create an unlimited number of them. + // + if (page == null) + page = new BoxViewClockPage (); + return page.GetOouiElement (); } class BoxViewClockPage : ContentPage diff --git a/Samples/XamarinFormsSample.cs b/Samples/XamarinFormsSample.cs index e555ac7..0adccdf 100644 --- a/Samples/XamarinFormsSample.cs +++ b/Samples/XamarinFormsSample.cs @@ -44,12 +44,12 @@ namespace Samples var page = MakePage (); page.Publish ("/xamarin-forms-shared"); - Ooui.UI.Publish ("/xamarin-forms", () => MakePage ().CreateElement ()); + Ooui.UI.Publish ("/xamarin-forms", () => MakePage ().GetOouiElement ()); } public Ooui.Element CreateElement () { - return MakePage ().CreateElement (); + return MakePage ().GetOouiElement (); } } }