From: Stephane Delcroix Date: Thu, 14 Feb 2019 19:08:16 +0000 (+0100) Subject: [C] Binding null on nullable (#5255) X-Git-Tag: accepted/tizen/5.5/unified/20200421.150457~500^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=748da1e628900efe2309cb5d4bd796c4f1eb30cd;p=platform%2Fcore%2Fcsapi%2Fxsf.git [C] Binding null on nullable (#5255) * 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 --- diff --git a/Xamarin.Forms.Core/BindingExpression.cs b/Xamarin.Forms.Core/BindingExpression.cs index 163b369..903d391 100644 --- a/Xamarin.Forms.Core/BindingExpression.cs +++ b/Xamarin.Forms.Core/BindingExpression.cs @@ -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 index 0000000..aba2aa3 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5242.xaml @@ -0,0 +1,8 @@ + + + diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5242.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5242.xaml.cs new file mode 100644 index 0000000..130ac4d --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5242.xaml.cs @@ -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); + } + } + + } +}