我没有让它在 xaml 中工作,而当我在后面的代码中使用转换和使用字符串格式时,它可以正常工作:
<ContentPage.Resources>
<ResourceDictionary>
<local:thousandsSeparatorConverter x:Key="thousandsSeparator"/>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<!-- Place new controls here -->
<Label Text="{Binding date, Converter={StaticResource thousandsSeparator}}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
</StackLayout>
和local:thousandsSeparatorConverter
:
public class thousandsSeparatorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string s = value as string;
double number = double.Parse(s);
// Gets a NumberFormatInfo associated with the en-US culture.
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
// Displays the same value with a blank as the separator.
nfi.NumberGroupSeparator = " ";
Console.WriteLine(number.ToString("N0", nfi));
string convertedNumber = number.ToString("N0", nfi);
return convertedNumber;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}