共用方式為


EventToCommandBehavior

EventToCommandBehaviorbehavior ,可讓使用者透過 叫用 Command Event。 其設計目的是將命令與未設計為支援命令之控件所公開的事件產生關聯。 它可讓您將控件上的任何任意事件對應至 Command。

重要

.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

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 x:Name="MyButton">
        <Button.Behaviors>
            <toolkit:EventToCommandBehavior
                EventName="Clicked"
                BindingContext="{Binding Path=BindingContext, Source={x:Reference MyButton}, x:DataType=Button}"
                Command="{Binding Source={x:Reference Page}, Path=BindingContext.MyCustomCommand, x:DataType=ContentPage}" />
        </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.使用泛型實作

藉由使用 實作,EventToCommandBehavior<T>EventArgs如果未CommandParameter設定 和 Converter 屬性,則會傳遞至 Command 屬性。 為了在 XAML 中參考 泛型型 別,我們需要使用 x:TypeArguments 指示詞。

下列範例示範如何使用泛型實作將 傳遞 WebNavigatedEventArgs 至 命令。

<WebView 
    Source="https://github.com"
    x:Name="MyWebView">
    <WebView.Behaviors>
        <toolkit:EventToCommandBehavior
            x:TypeArguments="WebNavigatedEventArgs"
            EventName="Navigated"
            BindingContext="{Binding Path=BindingContext, Source={x:Reference MyWebView}, x:DataType=WebView}"
            Command="{Binding WebViewNavigatedCommand}" />
    </WebView.Behaviors>
</WebView>

2.使用 Converter 屬性

需要搭配選取專案或點選其他轉換器所ListView公開的事件使用此選項behavior。 這個轉換器會將事件自變數轉換成命令參數,然後傳遞至 Command。 它們也可以在 .NET MAUI 社群工具組中取得:

屬性

屬性 類型​ 描述
EventName string 應該與 Command相關聯的事件名稱。
Command ICommand Command應該執行的 。
CommandParameter object 要轉送至 的 Command選擇性參數。
EventArgsConverter IValueConverter 選擇性 IValueConverter ,可用來將值轉換成 EventArgs 傳入的值 Command

範例

您可以在 .NET MAUI Community Toolkit 範例應用程式中找到此行為的範例。

API

您可以在 .NET MAUI Community Toolkit GitHub 存放庫找到 的EventToCommandBehavior原始程式碼。