Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix input for decimal/float/double and nullable (#11815)
Browse files Browse the repository at this point in the history
* Fix input for decimal/float/double and nullable

This commit fixes the input of decimal/float/double and their
nullable equivalents in different cultures.

Issue #7996

* Update UI test

Makes the UI test more understandable. It shows now a label with the actual resolved binding value. In the entry you can now see the value you provided.

* Fix unit test

* Fix more unit tests

Co-authored-by: Gerald Versluis <gerald@verslu.is>
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
  • Loading branch information
3 people authored Jan 14, 2022
1 parent 37589f0 commit a13a484
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.ComponentModel;
using System.Globalization;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{

[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 7996, "Xamarin.Forms.Entry does not enter decimal when binding a float/double and decimal to it", PlatformAffected.Default)]
public class Issue7996 : TestContentPage
{
protected override void Init()
{
var vm = new ViewModelIssue7996();
BindingContext = vm;
var textLabel1 = new Label
{
Text = "Select culture:"
};
var picker = new Picker();
picker.ItemsSource = new[]
{
new CultureInfo("de"),
new CultureInfo("en"),
};
picker.SelectedIndexChanged += (s, e) =>
{
if (picker.SelectedItem is CultureInfo culture)
{
CultureInfo.CurrentUICulture = culture;
}
};

var textLabel2 = new Label
{
Text = "Resolved Binding Value:"
};
var bindingLable = new Label();
bindingLable.SetBinding(Label.TextProperty, new Binding(nameof(ViewModelIssue7996.MyDecimal), BindingMode.TwoWay));

var textLabel3 = new Label
{
Text = "Actual Value:"
};
var bindingLable2 = new Label();
bindingLable2.SetBinding(Label.TextProperty, new Binding(nameof(ViewModelIssue7996.MyDecimal), BindingMode.TwoWay));

var textLabel4 = new Label
{
Text = "Enter a number and watch result:"
};
var entry = new Entry();
entry.Text = vm.MyDecimal.ToString();
entry.TextChanged += (s, e) =>
{
bindingLable.Text = e.NewTextValue;
};

var stackLayout = new StackLayout
{
Children =
{
textLabel1,
picker,
textLabel2,
bindingLable,
textLabel3,
bindingLable2,
textLabel4,
entry,
}
};

Content = stackLayout;
}
}

[Preserve(AllMembers = true)]
public class ViewModelIssue7996 : INotifyPropertyChanged
{

decimal? myDecimal = 4.2m;

public event PropertyChangedEventHandler PropertyChanged;

public decimal? MyDecimal
{
get
{
return myDecimal;
}
set
{
myDecimal = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyDecimal)));
}
}

public ViewModelIssue7996()
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue14544.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Github7996.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14657.cs" />
<Compile Include="$(MSBuildThisFileDirectory)_TemplateMarkup.xaml.cs">
<DependentUpon>_TemplateMarkup.xaml</DependentUpon>
Expand Down
48 changes: 48 additions & 0 deletions Xamarin.Forms.Core.UnitTests/BindingExpressionTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -80,5 +81,52 @@ public void ValidPaths(
var binding = new Binding(path);
Assert.DoesNotThrow(() => new BindingExpression(binding, path));
}

static object[] TryConvertWithNumbersAndCulturesCases => new object[]
{
new object[]{ "4.2", new CultureInfo("en"), 4.2m },
new object[]{ "4,2", new CultureInfo("de"), 4.2m },
new object[]{ "-4.2", new CultureInfo("en"), -4.2m },
new object[]{ "-4,2", new CultureInfo("de"), -4.2m },

new object[]{ "4.2", new CultureInfo("en"), new decimal?(4.2m)},
new object[]{ "4,2", new CultureInfo("de"), new decimal?(4.2m) },
new object[]{ "-4.2", new CultureInfo("en"), new decimal?(-4.2m)},
new object[]{ "-4,2", new CultureInfo("de"), new decimal?(-4.2m) },

new object[]{ "4.2", new CultureInfo("en"), 4.2d },
new object[]{ "4,2", new CultureInfo("de"), 4.2d },
new object[]{ "-4.2", new CultureInfo("en"), -4.2d },
new object[]{ "-4,2", new CultureInfo("de"), -4.2d },

new object[]{ "4.2", new CultureInfo("en"), new double?(4.2d)},
new object[]{ "4,2", new CultureInfo("de"), new double?(4.2d) },
new object[]{ "-4.2", new CultureInfo("en"), new double?(-4.2d)},
new object[]{ "-4,2", new CultureInfo("de"), new double?(-4.2d) },

new object[]{ "4.2", new CultureInfo("en"), 4.2f },
new object[]{ "4,2", new CultureInfo("de"), 4.2f },
new object[]{ "-4.2", new CultureInfo("en"), -4.2f },
new object[]{ "-4,2", new CultureInfo("de"), -4.2f },

new object[]{ "4.2", new CultureInfo("en"), new float?(4.2f)},
new object[]{ "4,2", new CultureInfo("de"), new float?(4.2f) },
new object[]{ "-4.2", new CultureInfo("en"), new float?(-4.2f)},
new object[]{ "-4,2", new CultureInfo("de"), new float?(-4.2f) },

new object[]{ "4.", new CultureInfo("en"), "4." },
new object[]{ "4,", new CultureInfo("de"), "4," },
new object[]{ "-0", new CultureInfo("en"), "-0" },
new object[]{ "-0", new CultureInfo("de"), "-0" },
};

[TestCaseSource(nameof(TryConvertWithNumbersAndCulturesCases))]
public void TryConvertWithNumbersAndCultures(object inputString, CultureInfo culture, object expected)
{
CultureInfo.CurrentUICulture = culture;
BindingExpression.TryConvert(ref inputString, Entry.TextProperty, expected.GetType(), false);

Assert.AreEqual(expected, inputString);
}
}
}
13 changes: 7 additions & 6 deletions Xamarin.Forms.Core.UnitTests/BindingUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1998,21 +1998,22 @@ public void Convert()
}

#if !WINDOWS_PHONE
[TestCase("en-US"), TestCase("pt-PT")]
public void ConvertIsCultureInvariant(string culture)
[TestCase("en-US", "0.5", 0.5, 0.9, "0.9")]
[TestCase("pt-PT", "0,5", 0.5, 0.9, "0,9")]
public void ConvertIsCultureInvariant(string culture, string sliderSetStringValue, double sliderExpectedDoubleValue, double sliderSetDoubleValue, string sliderExpectedStringValue)
{
System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

var slider = new Slider();
var vm = new MockViewModel { Text = "0.5" };
var vm = new MockViewModel { Text = sliderSetStringValue };
slider.BindingContext = vm;
slider.SetBinding(Slider.ValueProperty, "Text", BindingMode.TwoWay);

Assert.That(slider.Value, Is.EqualTo(0.5));
Assert.That(slider.Value, Is.EqualTo(sliderExpectedDoubleValue));

slider.Value = 0.9;
slider.Value = sliderSetDoubleValue;

Assert.That(vm.Text, Is.EqualTo("0.9"));
Assert.That(vm.Text, Is.EqualTo(sliderExpectedStringValue));
}
#endif

Expand Down
13 changes: 7 additions & 6 deletions Xamarin.Forms.Core.UnitTests/TypedBindingUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,21 +1270,22 @@ public void Convert()
}

#if !WINDOWS_PHONE
[TestCase("en-US"), TestCase("pt-PT")]
public void ConvertIsCultureInvariant(string culture)
[TestCase("en-US", "0.5", 0.5, 0.9, "0.9")]
[TestCase("pt-PT", "0,5", 0.5, 0.9, "0,9")]
public void ConvertIsCultureInvariant(string culture, string sliderSetStringValue, double sliderExpectedDoubleValue, double sliderSetDoubleValue, string sliderExpectedStringValue)
{
System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

var slider = new Slider();
var vm = new MockViewModel { Text = "0.5" };
var vm = new MockViewModel { Text = sliderSetStringValue };
slider.BindingContext = vm;
slider.SetBinding(Slider.ValueProperty, new TypedBinding<MockViewModel, string>(mvm => mvm.Text, (mvm, s) => mvm.Text = s, null) { Mode = BindingMode.TwoWay });

Assert.That(slider.Value, Is.EqualTo(0.5));
Assert.That(slider.Value, Is.EqualTo(sliderExpectedDoubleValue));

slider.Value = 0.9;
slider.Value = sliderSetDoubleValue;

Assert.That(vm.Text, Is.EqualTo("0.9"));
Assert.That(vm.Text, Is.EqualTo(sliderExpectedStringValue));
}
#endif

Expand Down
10 changes: 5 additions & 5 deletions Xamarin.Forms.Core/BindingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,14 @@ internal static bool TryConvert(ref object value, BindableProperty targetPropert
}

object original = value;
try
try
{
convertTo = Nullable.GetUnderlyingType(convertTo) ?? convertTo;

var stringValue = value as string ?? string.Empty;
// see: https://bugzilla.xamarin.com/show_bug.cgi?id=32871
// do not canonicalize "*.[.]"; "1." should not update bound BindableProperty
if (stringValue.EndsWith(".", StringComparison.Ordinal) && DecimalTypes.Contains(convertTo))
{
if (stringValue.EndsWith(CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator, StringComparison.Ordinal) && DecimalTypes.Contains(convertTo)) {
value = original;
return false;
}
Expand All @@ -473,9 +474,8 @@ internal static bool TryConvert(ref object value, BindableProperty targetPropert
return false;
}

convertTo = Nullable.GetUnderlyingType(convertTo) ?? convertTo;
value = Convert.ChangeType(value, convertTo, CultureInfo.CurrentUICulture);

value = Convert.ChangeType(value, convertTo, CultureInfo.InvariantCulture);
return true;
}
catch (Exception ex) when (ex is InvalidCastException || ex is FormatException || ex is InvalidOperationException || ex is OverflowException)
Expand Down

0 comments on commit a13a484

Please sign in to comment.