UserStoppedTypingBehavior
UserStoppedTypingBehavior
est un Behavior
qui se déclenche lorsqu’un utilisateur a arrêté l’entrée de données sur les contrôles, par exemple Entry
, SearchBar
et Editor
. Son utilisation inclut, entre autres, le déclenchement d’une recherche lorsqu’un utilisateur a cessé d’entrer sa requête de recherche.
Important
Les comportements du Kit d’outils de la communauté .NET MAUI ne définissent pas le BindingContext
d’un comportement, car ces comportements peuvent être partagés et appliqués à plusieurs contrôles par l’intermédiaire de styles. Pour obtenir plus d’informations, voir Comportements MAUI .NET
Syntaxe
XAML
Y compris l’espace de noms XAML
Pour utiliser le kit de ressources dans XAML, le xmlns
suivant doit être ajouté à votre page ou à votre affichage :
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Il en résulte ce qui suit :
<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>
Serait modifié pour inclure le xmlns
de la manière suivante :
<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>
Utiliser UserStoppedTypingBehavior
Le UserStoppedTypingBehavior
peut être utilisé de la manière suivante dans 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">
<Entry
Placeholder="Start typing when you stop the behavior will trigger..."
x:Name="UserStoppedTypingEntry">
<Entry.Behaviors>
<toolkit:UserStoppedTypingBehavior
BindingContext="{Binding Path=BindingContext, Source={x:Reference UserStoppedTypingEntry}, x:DataType=Entry}"
Command="{Binding SearchCommand}"
StoppedTypingTimeThreshold="1000"
MinimumLengthThreshold="3"
ShouldDismissKeyboardAutomatically="True" />
</Entry.Behaviors>
</Entry>
</ContentPage>
C#
Le UserStoppedTypingBehavior
peut être utilisé de la manière suivante dans C# :
class UserStoppedTypingBehaviorPage : ContentPage
{
public UserStoppedTypingBehaviorPage()
{
var behavior = new UserStoppedTypingBehavior()
{
StoppedTypingTimeThreshold = 1000,
MinimumLengthThreshold = 3,
ShouldDismissKeyboardAutomatically = true
};
behavior.SetBinding(UserStoppedTypingBehavior.CommandProperty,
static (ViewModel vm) => vm.SearchCommand,
source: this.BindingContext);
var entry = new Entry
{
Placeholder = "Start typing when you stop the behavior will trigger..."
};
entry.Behaviors.Add(behavior);
}
}
Balisage C#
Notre package CommunityToolkit.Maui.Markup
permet d’utiliser ce Behavior
de manière beaucoup plus concise en C#.
using CommunityToolkit.Maui.Markup;
class UserStoppedTypingBehaviorPage : ContentPage
{
public UserStoppedTypingBehaviorPage()
{
Content = new Entry
{
Placeholder = "Start typing when you stop the behavior will trigger..."
}
.Behaviors(new UserStoppedTypingBehavior
{
StoppedTypingTimeThreshold = 1000,
MinimumLengthThreshold = 3,
ShouldDismissKeyboardAutomatically = true
}.Bind(UserStoppedTypingBehavior.CommandProperty,
getter: static (ViewModel vm) => vm.SearchCommand,
source: this.BindingContext,
mode: BindingMode.OneTime));
}
}
Propriétés
Propriété | Type | Description |
---|---|---|
Commande | ICommand | Commande à exécuter lorsque l’utilisateur a cessé de fournir une entrée. |
MinimumLengthThreshold | int | Longueur minimale de la valeur d’entrée requise avant l’exécution de la commande. |
ShouldDismissKeyboardAutomatically | bool | Indique si le clavier doit être ignoré automatiquement ou non. |
StoppedTypingTimeThreshold | int | Durée d’inactivité en millisecondes après laquelle la commande sera exécutée. |
Exemples
Vous trouverez un exemple de ce comportement en action dans l’Exemple d’application du kit de ressources de la communauté .NET MAUI.
API
Vous pouvez trouver le code source deUserStoppedTypingBehavior
sur le référentiel du kit de ressources de la communauté .NET MAUI sur GitHub.
.NET MAUI Community Toolkit