Entries in .NET MAUI

The Entry control in .NET MAUI is used for single-line text input.

Note: For multi-line text input, consider using the Editor control.

Basic Usage

The simplest way to use an Entry is to declare it in XAML or create it programmatically:

<Entry Placeholder="Enter text here" />

In C#:

var entry = new Entry
{
    Placeholder = "Enter text here"
};

Common Properties

The Entry control offers several properties to customize its appearance and behavior:

Property Description Default
Text Gets or sets the text entered by the user. Empty string
Placeholder Gets or sets the text displayed when the Entry is empty. Empty string
PlaceholderColor Gets or sets the color of the placeholder text. Platform-specific
Keyboard Gets or sets the type of keyboard to display. Keyboard.Default
IsPassword Gets or sets whether the text entered should be obscured (for password fields). false
ClearButtonVisibility Controls the visibility of the clear button. Platform-specific
MaxLength Gets or sets the maximum number of characters that can be entered. Unrestricted
HorizontalTextAlignment Gets or sets the horizontal alignment of the text. TextAlignment.Start
VerticalTextAlignment Gets or sets the vertical alignment of the text. TextAlignment.Center

Keyboard Types

You can specify different keyboard types using the Keyboard property:

Example: Numeric Keyboard and Password Field

<Entry Placeholder="Enter your age" Keyboard="Numeric" />
<Entry Placeholder="Enter your password" IsPassword="True" />

Handling Text Changes

You can respond to changes in the Entry's text using the TextChanged event.

Example: Binding Text and Handling Changes

XAML:

<Entry Text="{Binding UserName}"
       Placeholder="Enter username"
       TextChanged="OnUsernameChanged" />

<Label Text="{Binding StatusMessage}" />

C# Code-Behind:

public partial class MyPage : ContentPage
{
    public MyPage()
    {
        InitializeComponent();
        // Assuming a ViewModel is set as BindingContext
        // BindingContext = new MyViewModel();
    }

    private void OnUsernameChanged(object sender, TextChangedEventArgs e)
    {
        // Access the new text: e.NewTextValue
        // Access the old text: e.OldTextValue
        var username = ((Entry)sender).Text;
        // Update ViewModel or perform other actions
        // For example, if you have a ViewModel property StatusMessage:
        // StatusMessage = $"Username changed to: {username}";
        System.Diagnostics.Debug.WriteLine($"Username changed from '{e.OldTextValue}' to '{e.NewTextValue}'");
    }
}

Styling Entries

You can style Entry controls using standard .NET MAUI styling techniques, including:

Example: Using Styles

In App.xaml or a Resource Dictionary:

<Style TargetType="Entry">
    <Setter Property="TextColor" Value="DarkSlateBlue" />
    <Setter Property="FontSize" Value="Medium" />
    <Setter Property="Margin" Value="10" />
    <Setter Property="HorizontalOptions" Value="FillAndExpand" />
</Style>

<Style x:Key="PasswordEntryStyle" TargetType="Entry">
    <Setter Property="IsPassword" Value="True" />
    <Setter Property="PlaceholderColor" Value="LightGray" />
    <Setter Property="BackgroundColor" Value="#f0f0f0" />
</Style>

Using the Styles:

<Entry Placeholder="Enter name" /> <!-- Uses default style -->
<Entry Placeholder="Enter password" Style="{StaticResource PasswordEntryStyle}" />

By leveraging these properties and events, you can effectively implement single-line text input fields in your .NET MAUI applications.