EventToCommandBehavior

EventToCommandBehavior は、ユーザーが Event を介して Command を呼び出せるようにする behavior です。 これは、Command をサポートするように設計されていないコントロールによって公開されるイベントに Command を関連付けるように設計されています。 これにより、コントロール上の任意のイベントを Command にマップできます。

重要

.NET MAUI Community Toolkit のビヘイビアーでは、ビヘイビアーの BindingContext は設定されません。ビヘイビアーはスタイルを利用して共有し、複数のコントロールに適用できるためです。 詳細については、「.NET MAUI のビヘイビアー」を参照してください

構文

次の例は、EventToCommandBehaviorButton コントロールに追加し、clicked イベントを処理する方法を示しています。

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>

EventToCommandBehavior の使用

EventToCommandBehavior は、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"
    x:Name="Page">
    
    <Button>
        <Button.Behaviors>
            <toolkit:EventToCommandBehavior
                EventName="Clicked"
                Command="{Binding Source={x:Reference Page}, Path=BindingContext.MyCustomCommand}" />
        </Button.Behaviors>
    </Button>
</ContentPage>

C#

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

class EventToCommandBehaviorPage : ContentPage
{
    public EventToCommandBehaviorPage()
    {
        var button = new Button();

        var behavior = new EventToCommandBehavior
        {
            EventName = nameof(Button.Clicked),
            Command = new MyCustomCommand()
        };

        button.Behaviors.Add(behavior);

        Content = entry;
    }
}

C# Markup

この CommunityToolkit.Maui.Markup パッケージを使うと、より簡潔な方法でこの Behavior を C# で使用できます。

using CommunityToolkit.Maui.Markup;

class EventToCommandBehaviorPage : ContentPage
{
    public EventToCommandBehaviorPage()
    {
        Content = new Button()
        .Behaviors(new EventToCommandBehavior
        {
            EventName = nameof(Button.Clicked),
            Command = new MyCustomCommand()
        });                 
    }
}

イベントから EventArgs にアクセスする

特定のイベントの EventArgsCommand に渡すことができます。 これを実現する方法は 2 つあります。

1.汎用実装を使う

EventToCommandBehavior<T> 実装を使うと、CommandParameterConverter の両プロパティが設定されていない場合、EventArgsCommand プロパティに渡されます。 XAML でジェネリック型を参照するには、x:TypeArguments ディレクティブを使う必要があります。

次の例は、汎用実装を使って WebNavigatedEventArgs をコマンドに渡す方法を示しています。

<WebView Source="https://github.com">
    <WebView.Behaviors>
        <toolkit:EventToCommandBehavior
            x:TypeArguments="WebNavigatedEventArgs"
            EventName="Navigated"
            Command="{Binding WebViewNavigatedCommand}" />
    </WebView.Behaviors>
</WebView>

2.Converter プロパティを使う

この behavior を、ListView によって公開されている選択またはタップ イベントと共に使う場合は、追加のコンバーターが必要です。 このコンバーターを使ってイベント引数をコマンド パラメーターに変換し、それを Command に渡します。 これらは .NET MAUI Community Toolkit でも使用できます。

Properties

プロパティ タイプ 説明
EventName string Command に関連付ける必要があるイベントの名前。
コマンド ICommand 実行する必要がある Command
CommandParameter オブジェクト Command に転送する省略可能なパラメーター。
EventArgsConverter IValueConverter EventArgs 値を Command に渡す値に変換するために使用できる省略可能な IValueConverter

このビヘイビアーの動作の例は .NET MAUI Community Toolkit サンプル アプリケーションで確認できます。

API

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