Fixes binding to nullable types (#6860)
authorAndrei Nitescu <nitescua@yahoo.com>
Mon, 15 Jul 2019 11:34:54 +0000 (14:34 +0300)
committerStephane Delcroix <stephane@delcroix.org>
Mon, 15 Jul 2019 11:34:54 +0000 (13:34 +0200)
-fixes #6280

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

index 1248d03..8563906 100644 (file)
@@ -449,6 +449,8 @@ namespace Xamarin.Forms
                                        return false;
                                }
 
+                               convertTo = Nullable.GetUnderlyingType(convertTo) ?? convertTo;
+
                                value = Convert.ChangeType(value, convertTo, CultureInfo.InvariantCulture);
                                return true;
                        }
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Issue6280.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Issue6280.xaml
new file mode 100644 (file)
index 0000000..6121f4c
--- /dev/null
@@ -0,0 +1,13 @@
+<?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:d="http://xamarin.com/schemas/2014/forms/design"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+             mc:Ignorable="d"
+             xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests"
+             x:Class="Xamarin.Forms.Xaml.UnitTests.Issue6280"
+             x:DataType="local:Issue6280ViewModel">
+    <Entry x:Name="_entry"
+           Text="{Binding NullableInt}"
+           VerticalOptions="Start" />
+</ContentPage>
\ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Issue6280.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Issue6280.xaml.cs
new file mode 100644 (file)
index 0000000..36c46c9
--- /dev/null
@@ -0,0 +1,35 @@
+using NUnit.Framework;
+using Xamarin.Forms.Core.UnitTests;
+
+namespace Xamarin.Forms.Xaml.UnitTests
+{
+       public partial class Issue6280 : ContentPage
+       {
+               public Issue6280() => InitializeComponent();
+               public Issue6280(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 vm = new Issue6280ViewModel();
+                               var page = new Issue6280(useCompiledXaml) { BindingContext = vm };
+                               page._entry.SetValueFromRenderer(Entry.TextProperty, 1);
+                               Assert.AreEqual(vm.NullableInt, 1);
+                       }
+               }
+       }
+
+       public class Issue6280ViewModel
+       {
+               public int? NullableInt { get; set; }
+       }
+}
\ No newline at end of file