我如何将字符串列表绑定到StringFormat Xamarin

时间:2018-08-07 17:40:04

标签: list xamarin xamarin.forms string-formatting

我可以通过与字符串列表绑定将多个值绑定为String格式吗?

                        Text="{Binding FaceFingerPrintRecognitionLabel, StringFormat='You will need to activate the {0} recognition function on your permitted phone and register at least one of your {0}s to control access to the permitted mobile'}"

在上面我可以绑定字符串列表并在文本中使用它吗?我无法做到,有办法吗?

2 个答案:

答案 0 :(得分:2)

使用ConverterConverterParameter的XAML:

<ContentPage
    ...
    xmlns:local="clr-namespace:NamespaceOfConverter"
    ...
    <ContentPage.Resources>
        <local:StringArrayConverter x:Key="stringArrayConverter" />
    </ContentPage.Resources>
    ...
    <Label Text="{Binding StringArrayArguments, 
                          Converter={StaticResource stringArrayConverter},
                          ConverterParameter='Arg1 {0} arg2 {1} arg3 {2}'}"/>

ViewModel示例:

public string[] StringArrayArguments { get; set; } = new string[] { "A", "B", "C" };

示例Converter的实现:

public class StringArrayConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is string[] strings && parameter is string format)
        {
            try
            {
                return string.Format(format, strings);
            }
            catch (Exception)
            {
            }
        }
        return string.Empty;
    }

    //Must implement this if Binding with Mode=TwoWay
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

答案 1 :(得分:1)

您在这里描述的方式无法做到。除非像您的示例中生成的那样,否则您希望在多个位置显示相同的值。但我认为您也要具有多个值。

可能最好的选择是使用值转换器。

像这样实施一个:

public YourObjectToDescriptionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var yourObject = value as YourObject;

        // Double check we don't have a casting error
        if (yourObject == null)
            return string.Empty;

        return $"You will need to activate the {yourObject.FirstString} recognition function on your permitted phone and register at least one of your {yourObject.SecondString}s to control access to the permitted mobile";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Generally not needed in this scenario
        return null;
    }

}

当然,YourObject可以是任何复杂的对象或字符串数​​组或任何您想要的东西。

您将需要在XAML中声明您的转换器。您可以直接在页面中执行此操作,如果需要,可以在多个地方使用App.xaml。这样做:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:DataBindingDemos"
         x:Class="DataBindingDemos.EnableButtonsPage"
         Title="Enable Buttons">
<ContentPage.Resources>
    <ResourceDictionary>
        <local:YourObjectToDescriptionConverter x:Key="yourConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

<!-- Your contents -->

</ContentPage>

并在这样的绑定中使用它:Text="{Binding FaceFingerPrintRecognitionObject, Converter={StaticResource yourConverter}}"

在文档页面上了解有关Xamarin.Forms中的值转换器的更多信息:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters#the-ivalueconverter-interface