Comportamento al raggiungimento della lunghezza massima

MaxLengthReachedBehavior è un oggetto Behavior che consente all'utente di attivare un'azione quando un utente ha raggiunto la lunghezza massima consentita in un oggetto InputView. Può attivare Command oppure un evento, a seconda dello scenario preferito dall'utente. Sia Command sia l'evento includeranno il testo risultante di InputView.

Inoltre, è possibile ignorare la tastiera quando viene raggiunta la lunghezza massima tramite la ShouldDismissKeyboardAutomatically proprietà che per impostazione predefinita è false.

Importante

I comportamenti di .NET MAUI Community Toolkit non impostano il BindingContext di un comportamento, perché i comportamenti possono essere condivisi e applicati a più controlli tramite stili. Per altre informazioni, vedere .NET MAUI Comportamenti

Syntax

XAML

Inclusione dello spazio dei nomi XAML

Per usare il toolkit in XAML, è necessario aggiungere le informazioni seguenti xmlns nella pagina o nella visualizzazione:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

Di conseguenza:

<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>

Verrà modificato in modo da includere l'oggetto xmlns come indicato di seguito:

<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>

Utilizzo di MaxLengthReachedBehavior

Può MaxLengthReachedBehavior essere usato come segue 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="CommunityToolkit.Maui.Sample.Pages.Behaviors.MaxLengthReachedBehaviorPage"
    x:Name="Page">

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

</ContentPage>

C#

Può MaxLengthReachedBehavior essere usato come indicato di seguito in 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,
            static (MaxLengthReachedBehaviorViewModel vm) => vm.MaxLengthReachedCommand,
            source: this.BindingContext);

        entry.Behaviors.Add(behavior);

        Content = entry;
    }
}

Linguaggio di markup C#

Il nostro CommunityToolkit.Maui.Markup pacchetto fornisce un modo molto più conciso per usare questo Behavior in C#.

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,
                getter: static (ViewModel vm) => vm.MaxLengthReachedCommand,
                source: this.BindingContext));
    }
}

Proprietà

Proprietà Type Descrizione
Command ICommand Comando eseguito quando l'utente ha raggiunto la lunghezza massima. Il parametro del comando conterrà il Text del InputView.
ShouldDismissKeyboardAutomatically bool Indica se la tastiera deve essere chiusa automaticamente quando viene raggiunta la lunghezza massima.

Events

Event Descrizione
MaxLengthReached Evento generato quando l'utente ha raggiunto la lunghezza massima. Gli argomenti dell'evento conterranno il Text del InputView.

Esempi

È possibile trovare un esempio di questo comportamento in azione nell'applicazione di esempio .NET MAUI Community Toolkit.

API

È possibile trovare il codice sorgente per MaxLengthReachedBehavior nel repository .NET MAUI Community Toolkit GitHub.