StringToListConverter
StringToListConverter
は、1 つ以上の区切りに基づいて入力文字列を分割することで一連の部分文字列を返す一方向のコンバーターです。
Convert
メソッドは、1 つ以上の区切りに基づいて入力文字列を分割することで一連の部分文字列を返します。
Note
区切りは、次の優先順位で指定することにご注意ください。
- コンバーター バインディングにおける
ConverterParameter
として指定します。これはSeparators
とSeparator
の両方のプロパティより優先されます。 - コンバーターで
Separators
プロパティとして指定します。これはSeparator
プロパティよりも優先されます。 - コンバーターで
Separator
プロパティとして指定します。
ConvertBack
メソッドはサポートされていません。 逆の動作については、ListToStringConverter
を参照してください。
BaseConverter プロパティ
基底クラス public abstract class BaseConverter
には、次のプロパティが実装されています。
プロパティ | 説明 |
---|---|
DefaultConvertReturnValue |
IValueConverter.Convert(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
DefaultConvertBackReturnValue |
IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
ICommunityToolkitValueConverter プロパティ
public interface ICommunityToolkitValueConverter
には次のプロパティが実装されています。
プロパティ | タイプ | 説明 |
---|---|---|
DefaultConvertReturnValue |
object? |
IValueConverter.Convert(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
DefaultConvertBackReturnValue |
object? |
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 の使用
StringToListConverter
は、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#
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(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
この 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 [] { ",", ".", ";" }
})
}
};
}
}
Properties
プロパティ | タイプ | 説明 |
---|---|---|
Separator | string |
着信文字列内の部分文字列を区切る文字列。 この値は、ConverterParameter と Separators の両方に置き換えられます。 ConverterParameter が null で、Separators が空の場合、この値が使用されます。 |
区切り文字 | IList<string> |
着信文字列内の部分文字列を区切る文字列。 この値は、ConverterParameter に置き換えられます。 ConverterParameter が null の場合、この値が使用されます。 |
SplitOptions | StringSplitOptions |
部分文字列をトリミングして空の部分文字列を含めるかどうかを指定する列挙値のビットごとの組み合わせ。 |
例
このコンバーターの動作の例は .NET MAUI Community Toolkit サンプル アプリケーションで確認できます。
API
StringToListConverter
のソース コードは、.NET MAUI Community Toolkit の GitHub リポジトリにあります。
.NET MAUI Community Toolkit