Share via


EventToCommandBehavior

EventToCommandBehaviorbehavior 사용자가 통해 Event호출 Command 할 수 있도록 하는 것입니다. 명령을 지원하도록 설계되지 않은 컨트롤에 의해 노출되는 이벤트에 명령을 연결하도록 설계되었습니다. 컨트롤의 임의의 이벤트를 Command에 매핑할 수 있습니다.

Important

동작을 공유하고 스타일을 통해 여러 컨트롤에 적용할 수 있으므로 .NET MAUI 커뮤니티 도구 키트 동작은 동작을 설정 BindingContext 하지 않습니다. 자세한 내용은 .NET MAUI 동작을 참조 하세요.

구문

다음 예제에서는 컨트롤에 추가 하 EventToCommandBehaviorButton 고 클릭 한 이벤트를 처리 하는 방법을 보여 줍니다.

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

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

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

CommunityToolkit.Maui.Markup 패키지는 C#에서 이를 Behavior 사용하는 훨씬 더 간결한 방법을 제공합니다.

using CommunityToolkit.Maui.Markup;

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

이벤트에서 EventArgs 액세스

특정 이벤트의 전달을 할 EventArgs 수 있습니다 Command. 두 가지 방법으로 이 작업을 수행할 수 있습니다.

1. 제네릭 구현 사용

구현 EventArgsEventToCommandBehavior<T> 사용하면 속성과 Converter 속성이 모두 설정되지 않은 경우 속성에 CommandCommandParameter 전달됩니다. 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 속성 사용

추가 변환기에서 노출하는 ListView 선택 또는 탭 이벤트와 함께 이 behavior 옵션을 사용하는 경우 필요합니다. 이 변환기는 이벤트 인수를 명령 매개 변수로 변환한 다음, Command에 전달됩니다. .NET MAUI 커뮤니티 도구 키트에서도 사용할 수 있습니다.

속성

속성 Type 설명
EventName string 에 연결되어야 하는 이벤트의 이름입니다 Command.
명령 Icommand Command 실행해야 하는 개체입니다.
CommandParameter 개체 으로 전달할 선택적 매개 변수입니다 Command.
EventArgsConverter IValueConverter 에 전달된 값으로 값을 변환 EventArgs 하는 데 사용할 수 있는 Command선택 사항 IValueConverter 입니다.

예제

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

API

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