次の方法で共有


ItemTappedEventArgsConverter

ItemTappedEventArgsConverter は、ユーザーが ItemTappedEventArgs オブジェクトから Item 値を抽出することを可能にするコンバーターです。 これはその後、EventToCommandBehavior と組み合わせて使用できます。

BaseConverter プロパティ

基底クラス public abstract class BaseConverter には、次のプロパティが実装されています。

プロパティ 説明
DefaultConvertReturnValue IValueConverter.Convert(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。
DefaultConvertBackReturnValue IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。

ICommunityToolkitValueConverter プロパティ

public interface ICommunityToolkitValueConverter には次のプロパティが実装されています。

プロパティ タイプ 説明
DefaultConvertReturnValue object? IValueConverter.Convert(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。
DefaultConvertBackReturnValue object? IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。

構文

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>

ItemTappedEventArgsConverter の使用

ItemTappedEventArgsConverter は、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="MyLittleApp.MainPage">
    <ContentPage.Resources>
         <ResourceDictionary>
             <toolkit:ItemTappedEventArgsConverter x:Key="ItemTappedEventArgsConverter" />
         </ResourceDictionary>
    </ContentPage.Resources>

    <VerticalStackLayout Padding="10">

        <Label
            Text="The ItemTappedEventArgsConverter is a converter that allows users to extract the Item value from an ItemTappedEventArgs object. It can subsequently be used in combination with EventToCommandBehavior."
            TextColor="{StaticResource NormalLabelTextColor}"
            Margin="0, 0, 0, 20" />

        <ListView
            BackgroundColor="Transparent"
            ItemsSource="{Binding Items}"
            SelectedItem="{Binding ItemSelected, Mode=TwoWay}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <VerticalStackLayout Margin="6">
                            <Label Text="{Binding Name, StringFormat='Name: {0}'}"/>
                        </VerticalStackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.Behaviors>
                <toolkit:EventToCommandBehavior EventName="ItemTapped"
                                                Command="{Binding ItemTappedCommand}"
                                                EventArgsConverter="{StaticResource ItemTappedEventArgsConverter}" />
            </ListView.Behaviors>
        </ListView>
    </VerticalStackLayout>
</ContentPage>

C#

ItemTappedEventArgsConverter は、C# では次のように使用できます。

class ItemTappedEventArgsConverterPage : ContentPage
{
    public ItemTappedEventArgsConverterPage()
    {
        var behavior = new EventToCommandBehavior
        {
            EventName = nameof(ListView.ItemTapped),
            EventArgsConverter = new ItemTappedEventArgsConverter()
        };
        behavior.SetBinding(EventToCommandBehavior.CommandProperty, nameof(ViewModel.ItemTappedCommand);

        var listView = new ListView 
        { 
            HasUnevenRows = true 
        };
        listView.SetBinding(ListView.ItemsSource, nameof(ViewModel.Items));
        listView.Behaviors.Add(behavior);

        Content = listView;
    }
}

C# Markup

この CommunityToolkit.Maui.Markup パッケージは、C# でこのコンバーターを使用するためのより簡潔な方法を提供します。

using CommunityToolkit.Maui.Markup;

class ItemTappedEventArgsConverterPage : ContentPage
{
    public ItemTappedEventArgsConverterPage()
    {
        Content = new ListView
        {
            HasUnevenRows = true
        }
        .Bind(
            ListView.ItemsSourceProperty,
            static (ViewModel vm) => vm.Items)
        .Behaviors(
            new EventToCommandBehavior
            {
                EventName = nameof(ListView.ItemTapped),
                EventArgsConverter = new ItemTappedEventArgsConverter()
            }
            .Bind(
                EventToCommandBehavior.CommandProperty, 
                static (ViewModel vm) => vm.ItemTappedCommand,
                mode: BindingMode.OneTime));
    }
}

このコンバーターの動作の例は「.NET MAUI Community Toolkit サンプル アプリケーション」で確認できます。

API

ItemTappedEventArgsConverter のソース コードは、.NET MAUI Community Toolkit の GitHub リポジトリにあります。