EventToCommandBehavior 是允许用户通过 Event 调用 Command 的一种 behavior。 它旨在将命令与未设计为支持命令的控件公开的事件相关联。 它允许将控件上的任意事件映射到命令。
重要
.NET MAUI 社区工具包行为不会设置行为的 BindingContext,因为可以通过样式共享行为,并将其应用于多个控件。 有关详细信息,请参阅 .NET MAUI 行为
语法
以下示例演示如何向 Button 控件添加 EventToCommandBehavior,然后处理单击的事件。
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 传递到 Command 属性,前提是未设置 CommandParameter 和 Converter 属性。 要在 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 属性
将此 behavior 与 ListView 公开的选择或点击事件一起使用时,需要额外的转换器。 此转换器将事件参数转换为命令参数,然后传递给 Command。 它们也在 .NET MAUI 社区工具包中提供:
属性
| 属性 | 类型 | 描述 |
|---|---|---|
| EventName | string | 应与 Command 关联的事件的名称。 |
| 命令 | ICommand | 应执行的 Command。 |
| CommandParameter | object | 要转发到 Command 的可选参数。 |
| EventArgsConverter | IValueConverter | 一个可选的 IValueConverter,可用于将 EventArgs 值转换为传递到 Command 的值。 |
示例
可以在 .NET MAUI 社区工具包示例应用程序中查找此行为的示例。
API
可以在 .NET MAUI 社区工具包 GitHub 存储库查看EventToCommandBehavior 的源代码