Add the factory for native object (#133)
author유리나/Common Platform Lab(SR)/Staff Engineer/삼성전자 <rina6350.you@samsung.com>
Tue, 14 Jan 2020 06:56:00 +0000 (15:56 +0900)
committer부정균/Common Platform Lab(SR)/Staff Engineer/삼성전자 <jk.pu@samsung.com>
Tue, 14 Jan 2020 06:56:00 +0000 (15:56 +0900)
* Add the factory for native object

* Call the Hide method

Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Forms.cs
Xamarin.Forms/Xamarin.Forms.Platform.Tizen/FormsApplication.cs
Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Native/NativeFactory.cs [new file with mode: 0644]
Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Renderers/ImageRenderer.cs
Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Renderers/LabelRenderer.cs
Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Renderers/LayoutRenderer.cs
Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Renderers/PageRenderer.cs

index d6dac3d..f71923c 100644 (file)
@@ -731,6 +731,7 @@ namespace Xamarin.Forms
                        {
                                System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle);
                        }
+                       global::Xamarin.Forms.Platform.Tizen.Native.NativeFactory.PrecreateNatives(window);
                }
        }
 
index 664c9b7..bf9dad4 100644 (file)
@@ -8,6 +8,7 @@ using Tizen.Applications;
 using Xamarin.Forms.Internals;
 using ELayout = ElmSharp.Layout;
 using DeviceOrientation = Xamarin.Forms.Internals.DeviceOrientation;
+using Xamarin.Forms.Platform.Tizen.Native;
 
 namespace Xamarin.Forms.Platform.Tizen
 {
@@ -150,6 +151,8 @@ namespace Xamarin.Forms.Platform.Tizen
 
                        _platform.HasAlpha = MainWindow.Alpha;
                        _platform.SetPage(page);
+
+                       Device.BeginInvokeOnMainThread(() => Native.NativeFactory.DeleteUnusedNative());
                }
 
                void InitializeWindow()
diff --git a/Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Native/NativeFactory.cs b/Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Native/NativeFactory.cs
new file mode 100644 (file)
index 0000000..a9211ca
--- /dev/null
@@ -0,0 +1,114 @@
+using System;
+using ElmSharp;
+using System.Collections.Generic;
+
+namespace Xamarin.Forms.Platform.Tizen.Native
+{
+       public static class NativeFactory
+       {
+               static List<Label> labels = new List<Label>();
+               static List<Image> images = new List<Image>();
+               static List<Canvas> canvases = new List<Canvas>();
+               static List<Page> pages = new List<Page>();
+               const int hfrequency = 6;
+               const int lfrequency = 3;
+
+               public static void PrecreateNatives(EvasObject window)
+               {
+                       for (int i = 0; i < hfrequency; i++)
+                       {
+                               var label = new Label(window);
+                               label.Hide();
+                               labels.Add(label);
+
+                               var image = new Image(window);
+                               image.Hide();
+                               images.Add(image);
+                       }
+
+                       for (int i = 0; i < lfrequency; i++)
+                       {
+                               var page = new Page(window);
+                               page.Hide();
+                               pages.Add(page);
+
+                               var canvas = new Canvas(window);
+                               canvas.Hide();
+                               canvases.Add(canvas);
+                       }
+               }
+
+               public static EvasObject GetNativeControl(Type type)
+               {
+                       if (type == typeof(Label))
+                       {
+                               if (labels.Count >= 1)
+                               {
+                                       Label native = labels[labels.Count - 1];
+                                       labels.RemoveAt(labels.Count - 1);
+                                       return native;
+                               }
+                               return new Label(Forms.NativeParent);
+                       }
+                       else if (type == typeof(Image))
+                       {
+                               if (images.Count >= 1)
+                               {
+                                       Image native = images[images.Count - 1];
+                                       images.RemoveAt(images.Count - 1);
+                                       return native;
+                               }
+                               return new Image(Forms.NativeParent);
+                       }
+                       else if (type == typeof(Canvas))
+                       {
+                               if (canvases.Count >= 1)
+                               {
+                                       Canvas native = canvases[canvases.Count - 1];
+                                       canvases.RemoveAt(canvases.Count - 1);
+                                       return native;
+                               }
+                               return new Canvas(Forms.NativeParent);
+                       }
+                       else if (type == typeof(Page))
+                       {
+                               if (pages.Count >= 1)
+                               {
+                                       Page native = pages[pages.Count - 1];
+                                       pages.RemoveAt(pages.Count - 1);
+                                       return native;
+                               }
+                               return new Page(Forms.NativeParent);
+                       }
+                       return Activator.CreateInstance(type, new[] { Forms.NativeParent }) as EvasObject;
+               }
+
+               public static void DeleteUnusedNative()
+               {
+                       for (int i=0; i< labels.Count; i++)
+                       {
+                               Label native = labels[labels.Count - 1];
+                               labels.RemoveAt(labels.Count - 1);
+                               native.Unrealize();
+                       }
+                       for (int i = 0; i < images.Count; i++)
+                       {
+                               Image native = images[images.Count - 1];
+                               images.RemoveAt(images.Count - 1);
+                               native.Unrealize();
+                       }
+                       for (int i = 0; i < pages.Count; i++)
+                       {
+                               Page native = pages[pages.Count - 1];
+                               pages.RemoveAt(pages.Count - 1);
+                               native.Unrealize();
+                       }
+                       for (int i = 0; i < canvases.Count; i++)
+                       {
+                               Canvas native = canvases[canvases.Count - 1];
+                               canvases.RemoveAt(canvases.Count - 1);
+                               native.Unrealize();
+                       }
+               }
+       }
+}
\ No newline at end of file
index 7dd65f6..8d6e668 100644 (file)
@@ -1,7 +1,7 @@
 using System.ComponentModel;
 using System.Threading;
 using System.Threading.Tasks;
-
+using Xamarin.Forms.Platform.Tizen.Native;
 using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.Image;
 
 namespace Xamarin.Forms.Platform.Tizen
@@ -12,7 +12,7 @@ namespace Xamarin.Forms.Platform.Tizen
                {
                        if (Control == null)
                        {
-                               var image = new Native.Image(Forms.NativeParent);
+                               var image = NativeFactory.GetNativeControl(typeof(Native.Image)) as Native.Image;
                                SetNativeControl(image);
                        }
 
index 2577df7..7b35473 100644 (file)
@@ -26,7 +26,7 @@ namespace Xamarin.Forms.Platform.Tizen
                {
                        if (Control == null)
                        {
-                               base.SetNativeControl(new Native.Label(Forms.NativeParent));
+                               base.SetNativeControl(NativeFactory.GetNativeControl(typeof(Native.Label)) as Native.Label);
                        }
                        base.OnElementChanged(e);
                }
index 32dfa30..be5452a 100644 (file)
@@ -24,7 +24,7 @@ namespace Xamarin.Forms.Platform.Tizen
                {
                        if (null == Control)
                        {
-                               SetNativeControl(new Native.Canvas(Forms.NativeParent));
+                               SetNativeControl(Native.NativeFactory.GetNativeControl(typeof(Native.Canvas)) as Native.Canvas);
                        }
 
                        base.OnElementChanged(e);
index a831753..6b6ec9e 100644 (file)
@@ -25,7 +25,7 @@ namespace Xamarin.Forms.Platform.Tizen
                {
                        if (null == _page)
                        {
-                               _page = new Native.Page(Forms.NativeParent);
+                               _page = Native.NativeFactory.GetNativeControl(typeof(Native.Page)) as Native.Page;
                                _page.LayoutUpdated += OnLayoutUpdated;
                                SetNativeView(_page);
                        }