Share via


MaxLengthReachedBehavior

사용자가 MaxLengthReachedBehaviorBehavior 허용된 최대 길이에 도달했을 때 사용자가 작업을 트리거할 수 InputView있도록 하는 것입니다. 사용자가 선호하는 시나리오에 따라 이벤트를 트리거하거나 트리거 Command 할 수 있습니다. Command 이벤트와 이벤트에는 모두 .의 결과 텍스트가 InputView포함됩니다.

또한 기본값인 속성을 통해 최대 길이에 도달하면 키보드를 ShouldDismissKeyboardAutomatically 해제할 수 false있습니다.

Important

동작을 공유하고 스타일을 통해 여러 컨트롤에 적용할 수 있으므로 .NET MAUI 커뮤니티 도구 키트 동작은 동작을 설정 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>

MaxLengthReachedBehavior 사용

XAML MaxLengthReachedBehavior 에서 다음과 같이 사용할 수 있습니다.

<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="CommunityToolkit.Maui.Sample.Pages.Behaviors.MaxLengthReachedBehaviorPage"
    x:Name="Page">

    <Entry Placeholder="Start typing until MaxLength is reached..."
           MaxLength="100">
        <Entry.Behaviors>
            <toolkit:MaxLengthReachedBehavior 
                Command="{Binding Source={x:Reference Page}, Path=BindingContext.MaxLengthReachedCommand}" />
        </Entry.Behaviors>
    </Entry>

</ContentPage>

C#

MaxLengthReachedBehavior C#에서 다음과 같이 사용할 수 있습니다.


class MaxLengthReachedBehaviorPage : ContentPage
{
    public MaxLengthReachedBehaviorPage()
    {
        var entry = new Entry
        {
            Placeholder = "Start typing until MaxLength is reached...",
            MaxLength = 100
        };

        var behavior = new MaxLengthReachedBehavior();
        behavior.SetBinding(
            MaxLengthReachedBehavior.CommandProperty,
            new Binding(
                nameof(ViewModel.MaxLengthReachedCommand)
            )
        );

        entry.Behaviors.Add(behavior);

        Content = entry;
    }
}

C# 태그

CommunityToolkit.Maui.Markup 패키지는 C#에서 이를 Behavior 사용하는 훨씬 더 간결한 방법을 제공합니다.

using CommunityToolkit.Maui.Markup;

class MaxLengthReachedBehaviorPage : ContentPage
{
    public MaxLengthReachedBehaviorPage()
    {
        Content = new Entry
        {
            Placeholder = "Start typing until MaxLength is reached...",
            MaxLength = 100
        }.Behaviors(
            new MaxLengthReachedBehavior().Bind(
                MaxLengthReachedBehavior.CommandProperty,
                static (ViewModel vm) => vm.MaxLengthReachedCommand));
    }
}

속성

속성 Type 설명
Command Icommand 사용자가 최대 길이에 도달했을 때 실행되는 명령입니다. 명령의 매개 변수에는 .의 매개 변수가 TextInputView포함됩니다.
ShouldDismissKeyboardAutomatically bool 최대 길이에 도달할 때 키보드를 자동으로 해제할지 여부를 나타냅니다.

이벤트

이벤트 설명
MaxLengthReached 사용자가 최대 길이에 도달했을 때 발생하는 이벤트입니다. 이벤트 인수에는 .의 인수가 TextInputView포함됩니다.

예제

.NET MAUI 커뮤니티 도구 키트 샘플 애플리케이션에서 이 동작의 예를 찾을 수 있습니다.

API

.NET MAUI 커뮤니티 도구 키트 GitHub 리포지토리에서 오버에 대한 MaxLengthReachedBehavior 소스 코드를 찾을 수 있습니다.