Share via


StringToListConverter

StringToListConverter 변환기는 하나 이상의 구분 기호에 따라 입력 문자열을 분할하여 부분 문자열 집합을 반환하는 단방향 변환기입니다.

이 메서드는 Convert 하나 이상의 구분 기호에 따라 입력 문자열을 분할하여 하위 문자열 집합을 반환합니다.

참고 항목

구분 기호는 다음 우선 순위 순서로 제공할 수 있습니다.

  1. ConverterParameter 변환기 바인딩의 Separators 대체되고 Separator 속성이 대체됩니다.
  2. 변환기에서 Separators 속성으로, 속성을 대체합니다 Separator .
  3. 변환기에서 Separator 속성으로 지정합니다.

ConvertBack 메서드는 지원되지 않습니다. 반대 동작은 을 참조하세요 ListToStringConverter.

BaseConverter 속성

다음 속성은 기본 클래스 public abstract class BaseConverter에서 구현됩니다.

속성 설명
DefaultConvertReturnValue 을 throw할 때 IValueConverter.Convert(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.
DefaultConvertBackReturnValue 을 throw할 때 IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.

ICommunityToolkitValueConverter 속성

다음 속성은 다음에서 구현됩니다.public interface ICommunityToolkitValueConverter

속성 Type 설명
DefaultConvertReturnValue object? 을 throw할 때 IValueConverter.Convert(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.
DefaultConvertBackReturnValue object? 을 throw할 때 IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.

구문

XAML

XAML 네임스페이스 포함

XAML에서 도구 키트를 사용하려면 페이지 또는 보기에 다음 xmlns 을 추가해야 합니다.

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

따라서 다음을 수행합니다.

<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>

다음과 같이 포함 xmlns 하도록 수정됩니다.

<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>

StringToListConverter 사용

XAML StringToListConverter 에서 다음과 같이 사용할 수 있습니다.

<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#

StringToListConverter C#에서 다음과 같이 사용할 수 있습니다.

class StringToListConverterPage : ContentPage
{
    public StringToListConverterPage()
    {
		var entry = new Entry { Placeholder = "Enter some text separated by ',' or '.' or ';'" };
		entry.SetBinding(Entry.TextProperty, new Binding(nameof(ViewModel.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(
				nameof(ViewModel.MyValue),
				converter: stringToListConverter));

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

C# 태그

CommunityToolkit.Maui.Markup 패키지는 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 [] { ",", ".", ";" }
                    })
            }
        };
    }
}

속성

속성 Type 설명
구분 기호 string 들어오는 문자열의 부분 문자열을 구분하는 문자열입니다. 이 값은 둘 다 ConverterParameter 로 대체됩니다 Separators. 비어 null 있는 Separators 경우 ConverterParameter 이 값이 사용됩니다.
구분 기호 IList<string> 들어오는 문자열의 부분 문자열을 구분하는 문자열입니다. 이 값은 .로 대체됩니다 ConverterParameter. 이 값이 사용 되는 경우 ConverterParameter 입니다 null .
SplitOptions StringSplitOptions 부분 문자열을 자르고 빈 부분 문자열을 포함할지를 지정하는 열거형 값의 비트 조합입니다.

예제

.NET MAUI 커뮤니티 도구 키트 샘플 애플리케이션에서 작동 중인 이 변환기의 예를 찾을 수 있습니다.

API

.NET MAUI 커뮤니티 도구 키트 GitHub 리포지토리에서 오버에 대한 StringToListConverter 소스 코드를 찾을 수 있습니다.