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

How can i bind list of string to StringFormat Xamarin

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

                        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'}"

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

按照你这里描述的方式,你做不到。除非像您的示例中产生的那样,您希望在多个地方显示相同的值。但我认为您也想拥有多个值。

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

像这样实现一个:

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

XAML 使用 ConverterConverterParameter:

<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}'}"/>

示例视图模型:

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();
    }
}