简体   繁体   English

将cultureinfo 设置为listview xamarin 形式?

[英]Set cultureinfo into listview xamarin forms?

I need to convert a currency value to the culture of a specific country.我需要将货币价值转换为特定国家/地区的文化。 In the code behind it works perfectly, but I don't know how to do it in a listview.在它背后的代码中完美地工作,但我不知道如何在列表视图中做到这一点。

Code behind背后的代码

TotalLabel.Text = MyDouble.ToString("C0", CultureInfo.CreateSpecificCulture("es-CL"));

How can I do it in a listview?我怎样才能在列表视图中做到这一点?

 <ListView.ItemTemplate >
                    <DataTemplate >
                        <ViewCell >
                                <Label Grid.Row="0" Grid.Column="2" Margin="0"   Text="{Binding Price,StringFormat='{0:C}'}" BackgroundColor="Transparent" HorizontalTextAlignment="End" HorizontalOptions="FillAndExpand" />
                                <Label Grid.Row="0" Grid.Column="3" Margin="0"   Text="{Binding Total,StringFormat='{0:C}'}" BackgroundColor="Transparent" HorizontalTextAlignment="End" HorizontalOptions="FillAndExpand" Style="{StaticResource entryReadOnlyStyle}"/>
                        </ViewCell>
                    </DataTemplate>
 </ListView.ItemTemplate>

Could you help me?.你可以帮帮我吗?。 Thank you谢谢

You can write a converter.你可以写一个转换器。 Here's an example of how to write and consume it in your XAML page.下面是如何在 XAML 页面中编写和使用它的示例。

First create a class called CurrencyCultureConverter or something like that首先创建一个名为CurrencyCultureConverter或类似的类

using System;
using System.Globalization;
using Xamarin.Forms;

namespace YourApp.Converters 
{
    public class CurrencyCultureConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.ToString("C0", CultureInfo.CreateSpecificCulture("es-CL"));
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Then in your XAML然后在你的 XAML 中

...
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converter="clr-namespace:YourApp.Converters;assembly=YourApp"
...

<ResourceDictionary>
    <converter:CurrencyCultureConverter x:Key="CurrencyCultureConverter" />
</ResourceDictionary>


<Label ... Text="{Binding Price,Converter={StaticResource CurrencyCultureConverter}}" BackgroundColor="Transparent" ... />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM