[C] Binding null on nullable (#5255)
authorStephane Delcroix <stephane@delcroix.org>
Thu, 14 Feb 2019 19:08:16 +0000 (20:08 +0100)
committerGitHub <noreply@github.com>
Thu, 14 Feb 2019 19:08:16 +0000 (20:08 +0100)
* fixes pull #4453

bindings should allow null for nullable valuetypes!

* [C] Binding null on nullable

@kicsiede's fix for #5242, plus unit test, targetted to 3.6.0

- closes #5242

Xamarin.Forms.Core/BindingExpression.cs
Xamarin.Forms.Xaml.UnitTests/Issues/Gh5242.xaml [new file with mode: 0644]
Xamarin.Forms.Xaml.UnitTests/Issues/Gh5242.xaml.cs [new file with mode: 0644]

index 163b369..903d391 100644 (file)
@@ -1,4 +1,4 @@
-using System;
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Diagnostics;
@@ -429,7 +429,7 @@ namespace Xamarin.Forms
                internal static bool TryConvert(ref object value, BindableProperty targetProperty, Type convertTo, bool toTarget)
                {
                        if (value == null)
-                               return !convertTo.GetTypeInfo().IsValueType;
+                               return !convertTo.GetTypeInfo().IsValueType || Nullable.GetUnderlyingType(convertTo) != null;
                        if ((toTarget && targetProperty.TryConvert(ref value)) || (!toTarget && convertTo.IsInstanceOfType(value)))
                                return true;
 
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5242.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5242.xaml
new file mode 100644 (file)
index 0000000..aba2aa3
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ContentPage
+        xmlns="http://xamarin.com/schemas/2014/forms"
+        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+        xmlns:local="using:Xamarin.Forms.Xaml.UnitTests"
+        x:Class="Xamarin.Forms.Xaml.UnitTests.Gh5242"
+        NullableInt="{Binding Value}" x:DataType="local:Gh5242VM">
+</ContentPage>
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5242.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5242.xaml.cs
new file mode 100644 (file)
index 0000000..130ac4d
--- /dev/null
@@ -0,0 +1,45 @@
+using System;
+using System.Collections.Generic;
+
+using NUnit.Framework;
+
+using Xamarin.Forms;
+using Xamarin.Forms.Core.UnitTests;
+
+namespace Xamarin.Forms.Xaml.UnitTests
+{
+       public class Gh5242VM
+       {
+               public int? Value {get;set;}
+       }
+
+       public partial class Gh5242 : ContentPage
+       {
+               public static readonly BindableProperty NullableIntProperty = BindableProperty.Create("NullableInt", typeof(int?), typeof(Gh5242), defaultValue:-1);
+               public int? NullableInt { get => (int?)GetValue(NullableIntProperty); }
+
+               public Gh5242() => InitializeComponent();
+               public Gh5242(bool useCompiledXaml)
+               {
+                       //this stub will be replaced at compile time
+               }
+
+               [TestFixture]
+               class Tests
+               {
+                       [SetUp] public void Setup() => Device.PlatformServices = new MockPlatformServices();
+                       [TearDown] public void TearDown() => Device.PlatformServices = null;
+
+                       [Test]
+                       public void BindingToNullable([Values (false, true)]bool useCompiledXaml)
+                       {
+                               var layout = new Gh5242(useCompiledXaml) {BindingContext = new Gh5242VM {Value = 42}};
+                               Assert.That(layout.NullableInt, Is.EqualTo(42));
+
+                               layout.BindingContext = new Gh5242VM { Value = null };
+                               Assert.That(layout.NullableInt, Is.Null);
+                       }
+               }
+
+       }
+}