Condividi tramite


StringToListConverter

StringToListConverter è un convertitore unidirezionale che restituisce un set di sottostringhe suddividendo la stringa di input in base a uno o più separatori.

Il Convert metodo restituisce un set di sottostringhe suddividendo la stringa di input in base a uno o più separatori.

Nota

Si noti che i separatori possono essere forniti nell'ordine di precedenza seguente:

  1. come nell'associazione ConverterParameter del convertitore; in questo modo vengono sostituite entrambe le Separators proprietà e Separator
  2. Separators come proprietà nel convertitore; in questo modo la Separator proprietà viene sostituita
  3. Separator come proprietà nel convertitore.

Il ConvertBack metodo non è supportato. Per il comportamento opposto, vedere .ListToStringConverter

Proprietà BaseConverter

Le proprietà seguenti vengono implementate nella classe base : public abstract class BaseConverter

Proprietà Descrizione
DefaultConvertReturnValue Valore predefinito da restituire quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) genera un'eccezione Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.
DefaultConvertBackReturnValue Valore predefinito da restituire quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) genera un'eccezione Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.

Proprietà ICommunityToolkitValueConverter

Le proprietà seguenti vengono implementate in public interface ICommunityToolkitValueConverter:

Proprietà Type Descrizione
DefaultConvertReturnValue object? Valore predefinito da restituire quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) genera un'eccezione Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.
DefaultConvertBackReturnValue object? Valore predefinito da restituire quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) genera un'eccezione Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.

Sintassi

XAML

Inclusione dello spazio dei nomi XAML

Per usare il toolkit in XAML, è necessario aggiungere le informazioni seguenti xmlns nella pagina o nella visualizzazione:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

Di conseguenza:

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

</ContentPage>

Verrà modificato in modo da includere l'oggetto xmlns come indicato di seguito:

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

</ContentPage>

Uso di StringToListConverter

Può StringToListConverter essere usato come segue in XAML:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="CommunityToolkit.Maui.Sample.Pages.Converters.StringToListConverterPage">

    <ContentPage.Resources>
        <ResourceDictionary>
             <toolkit:StringToListConverter x:Key="StringToListConverter" SplitOptions="RemoveEmptyEntries">
                <toolkit:StringToListConverter.Separators>
                    <x:String>,</x:String>
                    <x:String>.</x:String>
                    <x:String>;</x:String>
                </toolkit:StringToListConverter.Separators>
            </toolkit:StringToListConverter>
        </ResourceDictionary>
    </ContentPage.Resources>

    <VerticalStackLayout>
        <Entry
            Placeholder="Enter some text separated by ',' or '.' or ';'"
            Text="{Binding MyValue}" />

        <CollectionView ItemsSource="{Binding MyValue, Converter={StaticResource StringToListConverter}}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding .}" />
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </VerticalStackLayout>

</ContentPage>

C#

Può StringToListConverter essere usato come indicato di seguito in C#:

class StringToListConverterPage : ContentPage
{
    public StringToListConverterPage()
    {
        var entry = new Entry { Placeholder = "Enter some text separated by ',' or '.' or ';'" };
        entry.SetBinding(Entry.TextProperty, new Binding(static (ViewModel vm) => vm.MyValue));

        var stringToListConverter = new StringToListConverter
        {
            SplitOptions = System.StringSplitOptions.RemoveEmptyEntries,
            Separators = new [] { ",", ".", ";" }
        };

        var collectionView = new CollectionView
        {
            ItemTemplate = new DataTemplate(() =>
            {
                var itemLabel = new Label();
                itemLabel.SetBinding(Label.TextProperty, path: ".");
                return itemLabel;
            })
        };

        collectionView.SetBinding(
            CollectionView.ItemsSourceProperty,
            new Binding(
                static (ViewModel vm) => vm.MyValue,
                converter: stringToListConverter));

        Content = new VerticalStackLayout
        {
            Children =    
            {
                entry,
                collectionView
            }
        };
    }
}

C# Markup

Il CommunityToolkit.Maui.Markup pacchetto offre un modo molto più conciso per usare questo convertitore in C#.

using CommunityToolkit.Maui.Markup;

class StringToListConverterPage : ContentPage
{
    public StringToListConverterPage()
    {
        Content = new VerticalStackLayout
        {
            Children =    
            {
                new Entry { Placeholder = "Enter some text separated by ',' or '.' or ';'" }
                    .Bind(
                        Entry.TextProperty, 
                        static (ViewModel vm) => vm.MyValue),

                new CollectionView
                {
                    ItemTemplate = new DataTemplate(() => new Label().Bind(Label.TextProperty, path: Binding.SelfPath))
                }.Bind(
                    CollectionView.ItemsSourceProperty,
                    static (ViewModel vm) => vm.MyValue,
                    converter: new StringToListConverter
                    {
                        SplitOptions = System.StringSplitOptions.RemoveEmptyEntries,
                        Separators = new [] { ",", ".", ";" }
                    })
            }
        };
    }
}

Proprietà

Proprietà Type Descrizione
Separatore string Stringa che delimita le sottostringhe nella stringa in ingresso. Questo valore viene sostituito sia da che Separatorsda ConverterParameter . Se ConverterParameter è e Separators è null vuoto, verrà usato questo valore.
Separatori IList<string> Stringhe che delimitano le sottostringhe nella stringa in ingresso. Questo valore viene sostituito da ConverterParameter. Se ConverterParameter è null questo valore verrà usato.
SplitOptions StringSplitOptions Combinazione bit per bit dei valori di enumerazione, che specifica se tagliare le sottostringhe e includere le sottostringhe vuote.

Esempi

È possibile trovare un esempio di questo convertitore in azione nell'applicazione di esempio .NET MAUI Community Toolkit.

API

È possibile trovare il codice sorgente per StringToListConverter over nel repository GitHub di .NET MAUI Community Toolkit.