diff options
author | Stephane Delcroix <stephane@delcroix.org> | 2016-09-26 22:32:19 +0200 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-09-26 13:32:19 -0700 |
commit | 16295a31393ff8ea0011a9837f709cb063a245a1 (patch) | |
tree | aad3553adcb9c1902ec5e1253d4e48cf79a583eb /Xamarin.Forms.Core | |
parent | 55f066584c507ec92d5054fac4f3a35f54c05522 (diff) | |
download | xamarin-forms-16295a31393ff8ea0011a9837f709cb063a245a1.tar.gz xamarin-forms-16295a31393ff8ea0011a9837f709cb063a245a1.tar.bz2 xamarin-forms-16295a31393ff8ea0011a9837f709cb063a245a1.zip |
[C] specify type and default value for native bindings (#376)
Diffstat (limited to 'Xamarin.Forms.Core')
-rw-r--r-- | Xamarin.Forms.Core/NativeBindingHelpers.cs | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/Xamarin.Forms.Core/NativeBindingHelpers.cs b/Xamarin.Forms.Core/NativeBindingHelpers.cs index c732efd5..7e2db65b 100644 --- a/Xamarin.Forms.Core/NativeBindingHelpers.cs +++ b/Xamarin.Forms.Core/NativeBindingHelpers.cs @@ -12,7 +12,7 @@ namespace Xamarin.Forms { static class NativeBindingHelpers { - public static void SetBinding<TNativeView>(TNativeView target, string targetProperty, BindingBase bindingBase, string updateSourceEventName=null) where TNativeView : class + public static void SetBinding<TNativeView>(TNativeView target, string targetProperty, BindingBase bindingBase, string updateSourceEventName = null) where TNativeView : class { var binding = bindingBase as Binding; //This will allow setting bindings from Xaml by reusing the MarkupExtension @@ -36,7 +36,9 @@ namespace Xamarin.Forms var proxy = BindableObjectProxy<TNativeView>.BindableObjectProxies.GetValue(target, (TNativeView key) => new BindableObjectProxy<TNativeView>(key)); BindableProperty bindableProperty = null; propertyChanged = propertyChanged ?? target as INotifyPropertyChanged; - bindableProperty = CreateBindableProperty<TNativeView>(targetProperty); + var propertyType = target.GetType().GetProperty(targetProperty)?.PropertyType; + var defaultValue = target.GetType().GetProperty(targetProperty)?.GetMethod.Invoke(target, new object [] { }); + bindableProperty = CreateBindableProperty<TNativeView>(targetProperty, propertyType, defaultValue); if (binding != null && binding.Mode != BindingMode.OneWay && propertyChanged != null) propertyChanged.PropertyChanged += (sender, e) => { if (e.PropertyName != targetProperty) @@ -50,17 +52,20 @@ namespace Xamarin.Forms proxy.SetBinding(bindableProperty, bindingBase); } - static BindableProperty CreateBindableProperty<TNativeView>(string targetProperty) where TNativeView : class + static BindableProperty CreateBindableProperty<TNativeView>(string targetProperty, Type propertyType = null, object defaultValue = null) where TNativeView : class { + propertyType = propertyType ?? typeof(object); + defaultValue = defaultValue ?? (propertyType.GetTypeInfo().IsValueType ? Activator.CreateInstance(propertyType) : null); return BindableProperty.Create( - targetProperty, - typeof(object), - typeof(BindableObjectProxy<TNativeView>), - defaultBindingMode: BindingMode.Default, - propertyChanged: (bindable, oldValue, newValue) => { - TNativeView nativeView; - if ((bindable as BindableObjectProxy<TNativeView>).TargetReference.TryGetTarget(out nativeView)) - SetNativeValue(nativeView, targetProperty, newValue); + targetProperty, + propertyType, + typeof(BindableObjectProxy<TNativeView>), + defaultValue: defaultValue, + defaultBindingMode: BindingMode.Default, + propertyChanged: (bindable, oldValue, newValue) => { + TNativeView nativeView; + if ((bindable as BindableObjectProxy<TNativeView>).TargetReference.TryGetTarget(out nativeView)) + SetNativeValue(nativeView, targetProperty, newValue); } ); } |