Fixes binding to nullable types (#6860)

-fixes #6280
This commit is contained in:
Andrei Nitescu 2019-07-15 14:34:54 +03:00 коммит произвёл Stephane Delcroix
Родитель 04d44fbe82
Коммит 802ab20e71
3 изменённых файлов: 50 добавлений и 0 удалений

Просмотреть файл

@ -449,6 +449,8 @@ namespace Xamarin.Forms
return false;
}
convertTo = Nullable.GetUnderlyingType(convertTo) ?? convertTo;
value = Convert.ChangeType(value, convertTo, CultureInfo.InvariantCulture);
return true;
}

Просмотреть файл

@ -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>

Просмотреть файл

@ -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; }
}
}