当语言设置为法语时,小数点分隔符“,”在绑定到双精度 属性 时被忽略

When language is set to French, decimal point separator ',' is ignored in binding to a double property

所以我有一个条目绑定到双 属性 和 Keyboard="Numeric"

问题是当我在 属性 setter 中键入“12,3”时,值等于 123。当然最后它在输入字段中显示 123属于“12,3”。

xaml

<Entry Keyboard="Numeric" Text="{Binding MyProperty}"/>

C#

private double _MyProp;

public double MyProperty
{
    get { return _MyProp; }
    set { SetProperty(ref _MyProp, value); }
}

如何解决这个问题?

似乎是 Xamarin.Forms issue,作为解决方法,使用值转换器:

<ContentPage.Resources>
    <app1:DecimalPointFixConverter x:Key="DecimalPointFixConverter"/>
</ContentPage.Resources>

<Entry Keyboard="Numeric" Text="{Binding Path=MyProperty, Converter={x:StaticResource DecimalPointFixConverter}}"/>

DecimalPointFixConverter.cs

public class DecimalPointFixConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (value as string)?.Replace(',', '.') ?? value;
    }
}