summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-09-23 22:05:14 +0200
committerJason Smith <jason.smith@xamarin.com>2016-09-26 13:32:32 -0700
commitdef2ba43b21e2c3c1ef57f14d3e0a4c516e8e0a9 (patch)
treeb0aa4e4494fd3862ef657c83bd88f6c669f8142e
parenta32b417811710844a9a82b3ed12aa6263e3dd599 (diff)
downloadxamarin-forms-def2ba43b21e2c3c1ef57f14d3e0a4c516e8e0a9.tar.gz
xamarin-forms-def2ba43b21e2c3c1ef57f14d3e0a4c516e8e0a9.tar.bz2
xamarin-forms-def2ba43b21e2c3c1ef57f14d3e0a4c516e8e0a9.zip
[C] specify type and default value for native bindings
-rw-r--r--Xamarin.Forms.Controls/GalleryPages/XamlNativeViews.xaml2
-rw-r--r--Xamarin.Forms.Core.UnitTests/NativeBindingTests.cs32
-rw-r--r--Xamarin.Forms.Core/NativeBindingHelpers.cs27
3 files changed, 38 insertions, 23 deletions
diff --git a/Xamarin.Forms.Controls/GalleryPages/XamlNativeViews.xaml b/Xamarin.Forms.Controls/GalleryPages/XamlNativeViews.xaml
index 7a1fb850..142c33b2 100644
--- a/Xamarin.Forms.Controls/GalleryPages/XamlNativeViews.xaml
+++ b/Xamarin.Forms.Controls/GalleryPages/XamlNativeViews.xaml
@@ -9,6 +9,6 @@
<ContentPage.Content>
<ios:UILabel Text="{Binding NativeText}" View.HorizontalOptions="Start"/>
<androidWidget:TextView Text="{Binding NativeText}" x:Arguments="{x:Static formsandroid:Forms.Context}" />
- <win:TextBlock Text="Foo"/>
+ <win:TextBlock Text="{Binding NativeText}"/>
</ContentPage.Content>
</ContentPage> \ No newline at end of file
diff --git a/Xamarin.Forms.Core.UnitTests/NativeBindingTests.cs b/Xamarin.Forms.Core.UnitTests/NativeBindingTests.cs
index 66577f63..c13608d8 100644
--- a/Xamarin.Forms.Core.UnitTests/NativeBindingTests.cs
+++ b/Xamarin.Forms.Core.UnitTests/NativeBindingTests.cs
@@ -13,6 +13,16 @@ namespace Xamarin.Forms.Core.UnitTests
public int Bar { get; set; }
public string Baz { get; set; }
+ string cantBeNull = "";
+ public string CantBeNull {
+ get { return cantBeNull; }
+ set {
+ if (value == null)
+ throw new NullReferenceException();
+ cantBeNull = value;
+ }
+ }
+
public void FireBazChanged()
{
BazChanged?.Invoke(this, new TappedEventArgs(null));
@@ -380,17 +390,6 @@ namespace Xamarin.Forms.Core.UnitTests
}
[Test]
- public void ThrowsNeedsConverter()
- {
- var nativeView = new MockNativeView();
- Assert.AreEqual(null, nativeView.Foo);
- Assert.AreEqual(0, nativeView.Bar);
- var vm = new MockVMForNativeBinding();
- nativeView.SetBinding("SelectedColor", new Binding("CColor"));
- Assert.Throws<ArgumentException>(() => nativeView.SetBindingContext(vm));
- }
-
- [Test]
public void TestConverterDoesNotThrow()
{
var nativeView = new MockNativeView();
@@ -482,5 +481,16 @@ namespace Xamarin.Forms.Core.UnitTests
nativeView.SetBindingContext(vm);
Assert.AreEqual("foobar", vm.FFoo);
}
+
+ [Test]
+ public void DoNotApplyNull()
+ {
+ var native = new MockNativeView();
+ Assert.NotNull(native.CantBeNull);
+ native.SetBinding("CantBeNull", new Binding("FFoo", BindingMode.TwoWay));
+ Assert.NotNull(native.CantBeNull);
+ native.SetBindingContext(new { FFoo = "foo" });
+ Assert.AreEqual("foo", native.CantBeNull);
+ }
}
} \ No newline at end of file
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);
}
);
}