Aracılığıyla paylaş


ItemTappedEventArgsConverter

ItemTappedEventArgsConverter, kullanıcıların bir nesneden Öğe değerini ayıklamasına olanak tanıyan bir ItemTappedEventArgs dönüştürücüdür. Daha sonra EventToCommandBehavior ile birlikte kullanılabilir.

BaseConverter Özellikleri

Aşağıdaki özellikler temel sınıfında public abstract class BaseConverteruygulanır:

Özellik Açıklama
DefaultConvertReturnValue Bir Exceptionoluştururken IValueConverter.Convert(object?, Type, object?, CultureInfo?) döndürülecek varsayılan değer. CommunityToolkit.Maui.Options olarak ayarlandığında truebu değer kullanılır..ShouldSuppressExceptionsInConverters
DefaultConvertBackReturnValue Bir Exceptionoluştururken IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) döndürülecek varsayılan değer. CommunityToolkit.Maui.Options olarak ayarlandığında truebu değer kullanılır..ShouldSuppressExceptionsInConverters

ICommunityToolkitValueConverter Özellikleri

aşağıdaki özellikler içinde public interface ICommunityToolkitValueConverteruygulanır:

Özellik Türü Açıklama
DefaultConvertReturnValue object? Bir Exceptionoluştururken IValueConverter.Convert(object?, Type, object?, CultureInfo?) döndürülecek varsayılan değer. CommunityToolkit.Maui.Options olarak ayarlandığında truebu değer kullanılır..ShouldSuppressExceptionsInConverters
DefaultConvertBackReturnValue object? Bir Exceptionoluştururken IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) döndürülecek varsayılan değer. CommunityToolkit.Maui.Options olarak ayarlandığında truebu değer kullanılır..ShouldSuppressExceptionsInConverters

Sözdizimi

XAML

XAML ad alanını dahil edin

Araç setini XAML'de kullanmak için sayfanıza veya görünümünüzde aşağıdakilerin xmlns eklenmesi gerekir:

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

Bu nedenle aşağıdakiler:

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

aşağıdakiler dahil xmlns edilecek şekilde değiştirilir:

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

ItemTappedEventArgsConverter XAML'de aşağıdaki gibi kullanılabilir:

<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# dilinde aşağıdaki gibi kullanılabilir:

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# İşaretlemeyi

Paketimiz CommunityToolkit.Maui.Markup , C# dilinde bu dönüştürücüsü kullanmak için çok daha kısa bir yol sağlar.

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

Örnekler

Bu dönüştürücüye ilişkin bir örneği .NET MAUI Community Toolkit Örnek Uygulamasında bulabilirsiniz.

API

üzerinde için ItemTappedEventArgsConverterkaynak kodunu .NET MAUI Community Toolkit GitHub deposunda bulabilirsiniz.