Add support for NativeViews specified in XAML
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Thu, 26 Jan 2017 12:14:55 +0000 (13:14 +0100)
committerKangho Hur <kangho.hur@samsung.com>
Mon, 10 Jul 2017 02:11:18 +0000 (11:11 +0900)
See: https://developer.xamarin.com/guides/xamarin-forms/user-interface/native-views/

Change-Id: I0a4315cccdaa208585f417db4f91240555a64a47
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
Xamarin.Forms.Platform.Tizen/EvasObjectWrapper.cs
Xamarin.Forms.Platform.Tizen/Extensions/NativeBindingExtensions.cs [new file with mode: 0644]
Xamarin.Forms.Platform.Tizen/NativeBindingService.cs [new file with mode: 0644]
Xamarin.Forms.Platform.Tizen/NativeValueConverterService.cs [new file with mode: 0644]
Xamarin.Forms.Platform.Tizen/Properties/AssemblyInfo.cs
Xamarin.Forms.Platform.Tizen/Xamarin.Forms.Platform.Tizen.csproj

index 97cf77f..526c473 100644 (file)
@@ -11,6 +11,8 @@ namespace Xamarin.Forms.Platform.Tizen
                {
                        EvasObject = obj;
                        MeasureDelegate = measureDelegate;
+
+                       obj.TransferBindablePropertiesToWrapper(this);
                }
 
                public EvasObject EvasObject
@@ -20,5 +22,13 @@ namespace Xamarin.Forms.Platform.Tizen
                }
 
                public MeasureDelegate MeasureDelegate { get; }
+
+               protected override void OnBindingContextChanged()
+               {
+                       // TODO: we should provide a delegate to obtain children of a Container object,
+                       //       however currently there is no way to get the list of children
+                       EvasObject.SetBindingContext(BindingContext);
+                       base.OnBindingContextChanged();
+               }
        }
 }
diff --git a/Xamarin.Forms.Platform.Tizen/Extensions/NativeBindingExtensions.cs b/Xamarin.Forms.Platform.Tizen/Extensions/NativeBindingExtensions.cs
new file mode 100644 (file)
index 0000000..b04894b
--- /dev/null
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+
+using EObject = ElmSharp.EvasObject;
+
+namespace Xamarin.Forms.Platform.Tizen
+{
+       public static class NativeBindingExtensions
+       {
+               public static void SetBinding(this EObject view, string propertyName, BindingBase binding, string updateSourceEventName = null)
+               {
+                       NativeBindingHelpers.SetBinding(view, propertyName, binding, updateSourceEventName);
+               }
+
+               public static void SetBinding(this EObject view, BindableProperty targetProperty, BindingBase binding)
+               {
+                       NativeBindingHelpers.SetBinding(view, targetProperty, binding);
+               }
+
+               public static void SetValue(this EObject target, BindableProperty targetProperty, object value)
+               {
+                       NativeBindingHelpers.SetValue(target, targetProperty, value);
+               }
+
+               public static void SetBindingContext(this EObject target, object bindingContext, Func<EObject, IEnumerable<EObject>> getChildren = null)
+               {
+                       NativeBindingHelpers.SetBindingContext(target, bindingContext, getChildren);
+               }
+
+               internal static void TransferBindablePropertiesToWrapper(this EObject target, View wrapper)
+               {
+                       NativeBindingHelpers.TransferBindablePropertiesToWrapper(target, wrapper);
+               }
+       }
+}
diff --git a/Xamarin.Forms.Platform.Tizen/NativeBindingService.cs b/Xamarin.Forms.Platform.Tizen/NativeBindingService.cs
new file mode 100644 (file)
index 0000000..945726e
--- /dev/null
@@ -0,0 +1,38 @@
+using System;
+
+using EObject = ElmSharp.EvasObject;
+
+namespace Xamarin.Forms.Platform.Tizen
+{
+       class NativeBindingService : Xaml.INativeBindingService
+       {
+               public bool TrySetBinding(object target, string propertyName, BindingBase binding)
+               {
+                       var view = target as EObject;
+                       if (view == null)
+                               return false;
+                       if (target.GetType().GetProperty(propertyName)?.GetMethod == null)
+                               return false;
+                       view.SetBinding(propertyName, binding);
+                       return true;
+               }
+
+               public bool TrySetBinding(object target, BindableProperty property, BindingBase binding)
+               {
+                       var view = target as EObject;
+                       if (view == null)
+                               return false;
+                       view.SetBinding(property, binding);
+                       return true;
+               }
+
+               public bool TrySetValue(object target, BindableProperty property, object value)
+               {
+                       var view = target as EObject;
+                       if (view == null)
+                               return false;
+                       view.SetValue(property, value);
+                       return true;
+               }
+       }
+}
diff --git a/Xamarin.Forms.Platform.Tizen/NativeValueConverterService.cs b/Xamarin.Forms.Platform.Tizen/NativeValueConverterService.cs
new file mode 100644 (file)
index 0000000..956079e
--- /dev/null
@@ -0,0 +1,20 @@
+using System;
+
+using EObject = ElmSharp.EvasObject;
+
+namespace Xamarin.Forms.Platform.Tizen
+{
+       class NativeValueConverterService : Xaml.INativeValueConverterService
+       {
+               public bool ConvertTo(object value, Type toType, out object nativeValue)
+               {
+                       nativeValue = null;
+                       if (typeof(EObject).IsInstanceOfType(value) && toType.IsAssignableFrom(typeof(View)))
+                       {
+                               nativeValue = ((EObject)value).ToView();
+                               return true;
+                       }
+                       return false;
+               }
+       }
+}
index 37e0221..0b7977f 100644 (file)
@@ -19,6 +19,8 @@ using Xamarin.Forms.Platform.Tizen;
 
 [assembly: Xamarin.Forms.Dependency(typeof(ResourcesProvider))]
 [assembly: Xamarin.Forms.Dependency(typeof(Deserializer))]
+[assembly: Xamarin.Forms.Dependency(typeof(NativeBindingService))]
+[assembly: Xamarin.Forms.Dependency(typeof(NativeValueConverterService))]
 
 [assembly: ExportRenderer(typeof(Layout), typeof(LayoutRenderer))]
 [assembly: ExportRenderer(typeof(ScrollView), typeof(ScrollViewRenderer))]
index 012512b..5c8ac95 100644 (file)
@@ -51,6 +51,7 @@
     <Compile Include="Extensions\ColorExtensions.cs" />
     <Compile Include="Extensions\KeyboardExtensions.cs" />
     <Compile Include="Extensions\LayoutExtensions.cs" />
+    <Compile Include="Extensions\NativeBindingExtensions.cs" />
     <Compile Include="Extensions\ScrollToPositionExtensions.cs" />
     <Compile Include="Extensions\TextAlignmentExtensions.cs" />
     <Compile Include="Forms.cs" />
@@ -91,6 +92,8 @@
     <Compile Include="Native\TextHelper.cs" />
     <Compile Include="Native\TimePicker.cs" />
     <Compile Include="Native\Window.cs" />
+    <Compile Include="NativeBindingService.cs" />
+    <Compile Include="NativeValueConverterService.cs" />
     <Compile Include="Platform.cs" />
     <Compile Include="PlatformEffect.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />