UserStoppedTypingBehavior
The UserStoppedTypingBehavior
is a Behavior
that will trigger an action when a user has stopped data input on controls for example Entry
, SearchBar
and Editor
. Examples of its usage include triggering a search when a user has stopped entering their search query.
Important
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
Syntax
XAML
Including the XAML namespace
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>
Using the UserStoppedTypingBehavior
The UserStoppedTypingBehavior
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">
<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#
The UserStoppedTypingBehavior
can be used as follows in 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);
}
}
C# Markup
Our CommunityToolkit.Maui.Markup
package provides a much more concise way to use this Behavior
in 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));
}
}
Properties
Property | Type | Description |
---|---|---|
Command | ICommand | The command to execute when the user has stopped providing input. |
MinimumLengthThreshold | int | The minimum length of the input value required before the command will be executed. |
ShouldDismissKeyboardAutomatically | bool | Indicates whether or not the keyboard should be dismissed automatically. |
StoppedTypingTimeThreshold | int | The time of inactivity in milliseconds after which the command will be executed. |
Examples
You can find an example of this behavior in action in the .NET MAUI Community Toolkit Sample Application.
API
You can find the source code for UserStoppedTypingBehavior
over on the .NET MAUI Community Toolkit GitHub repository.
.NET MAUI Community Toolkit