如何从xamarin表单中的类连接字符串?

问题描述 投票:0回答:2

我的listview中有一个标签

<Label Text="{Binding Price,StringFormat='GlobalVariable.Currency{0:F0}'}" 

结果应为:0.00美元

我想把货币汇总到价格

我的全局变量类:

public Static Class GlobalVariable
{
  Currency="$";
}

//货币可以改变。

那么如何将货币从一个类连接到xaml?

xamarin xamarin.forms
2个回答
2
投票

开箱即用是不可能的。有关更多信息,请参阅此topic:如果我是你,我会:

  1. ViewModel图层上的格式字符串,
  2. 使用ValueConverter转换值。

1
投票

你可以使用转换器:

public class CurrencyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string value = value as string;
        return GlobalVariable + value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
       throw new NotImplementedException();
    }
}

在你的Page.xaml中

 <ContentPage.Resources>
        <ResourceDictionary>
            <converter:CurrencyConverter x:Key="currencyConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

然后,绑定

<Label Text="{Binding Price,Converte={StaticResource currencyConverter}}" />
© www.soinside.com 2019 - 2024. All rights reserved.