Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
SetFocusOnEntryCompletedBehavior è un Behavior che assegna lo stato attivo a un VisualElement specificato quando un Entry viene completato. Ad esempio, una pagina potrebbe avere diversi Entry in sequenza, e questo può risultare comodo per l'utente se, una volta completato un Entry, il focus passasse automaticamente al successivo Entry.
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
Gli esempi seguenti mostrano come aggiungere il SetFocusOnEntryCompletedBehavior a un Entry in modo che, quando si preme il pulsante Next sulla tastiera virtuale, venga impostato il focus su un altro Entry.
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>
Uso di SetFocusOnEntryCompletedBehavior
Può SetFocusOnEntryCompletedBehavior essere usato come segue in XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.Behaviors.SetFocusOnEntryCompletedBehaviorPage"
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">
<VerticalStackLayout Spacing="12">
<Entry
x:Name="FirstNameEntry"
toolkit:SetFocusOnEntryCompletedBehavior.NextElement="{x:Reference LastNameEntry}"
Placeholder="Entry 1 (Tap `Next` on the keyboard when finished)"
ReturnType="Next" />
<Entry
x:Name="LastNameEntry" />
</VerticalStackLayout>
</ContentPage>
C#
Può SetFocusOnEntryCompletedBehavior essere usato come indicato di seguito in C#:
class SetFocusOnEntryCompletedBehaviorPage : ContentPage
{
public SetFocusOnEntryCompletedBehaviorPage()
{
var firstName = new Entry
{
Placeholder = "Entry 1 (Tap `Next` on the keyboard when finished)",
ReturnType = ReturnType.Next
};
var lastName = new Entry();
SetFocusOnEntryCompletedBehavior.SetNextElement(firstName, lastName);
Content = new VerticalStackLayout
{
Spacing = 12,
Children =
{
firstName,
lastName
}
};
}
}
Linguaggio di markup C#
Il CommunityToolkit.Maui.Markup pacchetto offre un modo molto più conciso per usare questo comportamento in C#.
using CommunityToolkit.Maui.Markup;
class SetFocusOnEntryCompletedBehaviorPage : ContentPage
{
public SetFocusOnEntryCompletedBehaviorPage()
{
Content = new VerticalStackLayout
{
Spacing = 12,
Children =
{
new Entry { ReturnType = ReturnType.Next }
.Assign(out var firstName)
.Placeholder("Entry 1 (Tap `Next` on the keyboard when finished)"),
new Entry()
.Assign(out var lastName)
}
};
SetFocusOnEntryCompletedBehavior.SetNextElement(firstName, lastName);
}
}
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 SetFocusOnEntryCompletedBehavior nel repository .NET MAUI Community Toolkit GitHub.
.NET MAUI Community Toolkit