Evenementer
Mar 17, 9 PM - Mar 21, 10 AM
Maacht mat bei der Meetup-Serie, fir skaléierbar KI-Léisungen op Basis vu realistesche Benotzungsfäll mat aneren Entwéckler an Experten ze bauen.
Elo umellenDëse Browser gëtt net méi ënnerstëtzt.
Upgrat op Microsoft Edge fir vun de Virdeeler vun leschten Eegeschaften, Sécherheetsupdaten, an techneschem Support ze profitéieren.
The EventToCommandBehavior
is a behavior
that allows the user to invoke a Command
through an Event
. It is designed to associate Commands to events exposed by controls that were not designed to support Commands. It allows you to map any arbitrary event on a control to a Command.
Wichteg
The .NET MAUI Community Toolkit Behaviors do not set the BindingContext
of a behavior, because behaviors can be shared and applied to multiple controls through styles. For more information refer to .NET MAUI Behaviors
The following examples show how to add an EventToCommandBehavior
to a Button
control and then handle the clicked event.
In order to use the toolkit in XAML the following xmlns
needs to be added into your page or view:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Therefore the following:
<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>
Would be modified to include the xmlns
as follows:
<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>
The EventToCommandBehavior
can be used as follows in 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>
The EventToCommandBehavior
can be used as follows in 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;
}
}
Our CommunityToolkit.Maui.Markup
package provides a much more concise way to use this Behavior
in C#.
using CommunityToolkit.Maui.Markup;
class EventToCommandBehaviorPage : ContentPage
{
public EventToCommandBehaviorPage()
{
Content = new Button()
.Behaviors(new EventToCommandBehavior
{
EventName = nameof(Button.Clicked),
Command = new MyCustomCommand()
});
}
}
It is possible to have the EventArgs
of the specific event passed into the Command
. There are two ways to achieve this:
By using the EventToCommandBehavior<T>
implementation the EventArgs
will be passed into the Command
property if both the CommandParameter
and Converter
properties are not set. In order to refer to the generic type in XAML we need to make use of the x:TypeArguments
directive.
The following example shows how to use the generic implementation to pass the WebNavigatedEventArgs
into the command.
<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>
When using this behavior
with selection or tap events exposed by ListView
an additional converter is required. This converter converts the event arguments to a command parameter which is then passed onto the Command
. They are also available in the .NET MAUI Community Toolkit:
Property | Type | Description |
---|---|---|
EventName | string | The name of the event that should be associated with a Command . |
Command | ICommand | The Command that should be executed. |
CommandParameter | object | An optional parameter to forward to the Command . |
EventArgsConverter | IValueConverter | An optional IValueConverter that can be used to convert EventArgs values to values passed into the Command . |
You can find an example of this behavior in action in the .NET MAUI Community Toolkit Sample Application.
You can find the source code for EventToCommandBehavior
over on the .NET MAUI Community Toolkit GitHub repository.
Feedback zu .NET MAUI Community Toolkit
.NET MAUI Community Toolkit ass en Open-Source-Projet. Wielt e Link, fir Feedback ze ginn:
Evenementer
Mar 17, 9 PM - Mar 21, 10 AM
Maacht mat bei der Meetup-Serie, fir skaléierbar KI-Léisungen op Basis vu realistesche Benotzungsfäll mat aneren Entwéckler an Experten ze bauen.
Elo umellenTraining
Modul
Create a UI that uses data binding in .NET MAUI. - Training
Create a UI with data binding. Your UI automatically updates based on the latest data, while the data updates in response to changes in the UI.
Dokumentatioun
TouchBehavior - .NET MAUI Community Toolkit - Community Toolkits for .NET
The TouchBehavior is a Behavior that provides the ability to interact with any VisualElement based on touch, mouse click and hover events.
Learn how to implement the Command property with .NET MAUI data bindings. The commanding interface provides an alternative approach to implementing commands that is suited to the MVVM architecture.
Behaviors - .NET MAUI Community Toolkit - Community Toolkits for .NET
The .NET MAUI Community Toolkit provides a collection of pre-built, reusable behaviors to make developers lives easier.