次の方法で共有


UserStoppedTypingBehavior

UserStoppedTypingBehavior は、EntrySearchBarEditor などのコントロールに対するデータ入力をユーザーが停止したときにアクションをトリガーする Behaviorです。 その使用例としては、ユーザーが検索クエリの入力を停止したときに検索をトリガーする方法があります。

重要

.NET MAUI Community Toolkit のビヘイビアーでは、ビヘイビアーの BindingContext は設定されません。ビヘイビアーはスタイルを利用して共有し、複数のコントロールに適用できるためです。 詳細については、「.NET MAUI のビヘイビアー」を参照してください

構文

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>

UserStoppedTypingBehavior の使用

UserStoppedTypingBehavior は、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#

UserStoppedTypingBehavior は、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

この CommunityToolkit.Maui.Markup パッケージを使うと、より簡潔な方法でこの Behavior を 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

プロパティ タイプ 説明
コマンド ICommand ユーザーが入力を停止したときに実行するコマンド。
MinimumLengthThreshold int コマンドを実行する前に必要な入力値の最小長。
ShouldDismissKeyboardAutomatically [bool] キーボードを自動的に閉じるかどうかを示します。
StoppedTypingTimeThreshold int コマンドが実行されるまでの非アクティブ時間 (ミリ秒単位)。

このビヘイビアーの動作の例は .NET MAUI Community Toolkit サンプル アプリケーションで確認できます。

API

UserStoppedTypingBehavior のソース コードは、.NET MAUI Community Toolkit の GitHub リポジトリにあります。