Switch to reusing renderers on Forms elements

This commit is contained in:
Frank A. Krueger 2017-11-10 10:14:28 -08:00
parent 5575578c2d
commit 4a51859da1
3 changed files with 21 additions and 5 deletions

View File

@ -16,16 +16,24 @@ namespace Xamarin.Forms
Ooui.UI.Publish (path, () => lazyPage.Value); 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) if (!Xamarin.Forms.Forms.IsInitialized)
throw new InvalidOperationException ("call Forms.Init() before this"); 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)) { if (!(page.RealParent is Application)) {
var app = new DefaultApplication (); var app = new DefaultApplication ();
app.MainPage = page; app.MainPage = page;
} }
var result = new Ooui.Forms.Platform (); var result = new Ooui.Forms.Platform ();
result.SetPage (page); result.SetPage (page);
return result.Element; return result.Element;

View File

@ -7,9 +7,17 @@ namespace Samples
{ {
public string Title => "Xamarin.Forms BoxViewClock"; public string Title => "Xamarin.Forms BoxViewClock";
BoxViewClockPage page;
public Ooui.Element CreateElement () 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 class BoxViewClockPage : ContentPage

View File

@ -44,12 +44,12 @@ namespace Samples
var page = MakePage (); var page = MakePage ();
page.Publish ("/xamarin-forms-shared"); page.Publish ("/xamarin-forms-shared");
Ooui.UI.Publish ("/xamarin-forms", () => MakePage ().CreateElement ()); Ooui.UI.Publish ("/xamarin-forms", () => MakePage ().GetOouiElement ());
} }
public Ooui.Element CreateElement () public Ooui.Element CreateElement ()
{ {
return MakePage ().CreateElement (); return MakePage ().GetOouiElement ();
} }
} }
} }