C# 当语言设置为法语时,小数点分隔符'';在绑定到double属性时被忽略

C# 当语言设置为法语时,小数点分隔符'';在绑定到double属性时被忽略,c#,xaml,xamarin.forms,localization,ivalueconverter,C#,Xaml,Xamarin.forms,Localization,Ivalueconverter,因此,我使用Keyboard=“Numeric”将一个条目绑定到一个double属性 不使用StringFormat,不在应用程序级别修改/强制区域性 确认在法语中小数点分隔符为“,”so“.”将不是允许的字符: CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator equals ',' 问题是,当我键入“12,3”时,在属性设置器中,该值等于123。当然,在最后,它在输入字段中显示123,而不是“12,3” xa

因此,我使用
Keyboard=“Numeric”
将一个条目绑定到一个double属性

  • 不使用
    StringFormat
    ,不在应用程序级别修改/强制区域性

  • 确认在法语中小数点分隔符为“,”so“.”将不是允许的字符:

    CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator equals ','
    
问题是,当我键入“12,3”时,在属性设置器中,该值等于123。当然,在最后,它在输入字段中显示123,而不是“12,3”

xaml 如何克服此问题?

作为一种解决方法,使用值转换器似乎是一种解决方案:

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

<Entry Keyboard="Numeric" Text="{Binding Path=MyProperty, Converter={x:StaticResource DecimalPointFixConverter}}"/>
这似乎是一个解决方案,使用值转换器:

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

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

<Entry Keyboard="Numeric" Text="{Binding Path=MyProperty, Converter={x:StaticResource DecimalPointFixConverter}}"/>
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;
    }
}