Entry Control

The .NET MAUI Entry control is a single-line text input control that allows users to enter and edit text.

Overview

The Entry control is ideal for collecting short text inputs from users, such as names, email addresses, or passwords. It provides a simple and familiar interface for text entry across different platforms.

Basic Usage

Here's a simple example of how to declare an Entry control in XAML:

<Entry Placeholder="Enter your text here" />

And in C#:

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

Key Properties

The Entry control has several properties that you can use to customize its appearance and behavior:

Property Description Default Value
Text Gets or sets the text displayed in the Entry. 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. System default
IsPassword Gets or sets a value indicating whether the text should be masked (e.g., for passwords). false
Keyboard Gets or sets the type of keyboard to display. Keyboard.Default
TextColor Gets or sets the color of the text. System default
FontAttributes Gets or sets the font attributes (e.g., Bold, Italic). FontAttributes.None
FontSize Gets or sets the size of the font. System default
HorizontalTextAlignment Gets or sets the horizontal alignment of the text. TextAlignment.Start
VerticalTextAlignment Gets or sets the vertical alignment of the text. TextAlignment.Center

Example with Properties

XAML Example:

<Entry
    Placeholder="Enter your email"
    PlaceholderColor="Gray"
    TextColor="DarkBlue"
    IsPassword="False"
    Keyboard="Email"
    FontSize="16"
    HorizontalTextAlignment="Start"
    VerticalTextAlignment="Center" />

C# Example:

var emailEntry = new Entry
{
    Placeholder = "Enter your email",
    PlaceholderColor = Colors.Gray,
    TextColor = Colors.DarkBlue,
    Keyboard = Keyboard.Email,
    FontSize = 16,
    HorizontalTextAlignment = TextAlignment.Start,
    VerticalTextAlignment = TextAlignment.Center
};

Handling Text Changes

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

XAML Example:

<Entry TextChanged="OnEntryTextChanged" />

C# Code-Behind:

void OnEntryTextChanged(object sender, TextChangedEventArgs e)
{
    string oldText = e.OldTextValue;
    string newText = e.NewTextValue;
    System.Diagnostics.Debug.WriteLine($"Text changed from '{oldText}' to '{newText}'");
}

Keyboard Types

The Keyboard property allows you to specify the type of keyboard that appears when the Entry control gains focus. Common values include:

Tip: Use the appropriate keyboard type to improve the user experience by providing relevant keys and input validation.

Customization

Beyond the basic properties, you can further customize the Entry's appearance using styles and handlers for platform-specific adjustments.

Styling

You can define styles in your App.xaml or a separate resource dictionary to reuse Entry configurations.

<Style TargetType="Entry">
    <Setter Property="TextColor" Value="Purple" />
    <Setter Property="FontSize" Value="18" />
    <Setter Property="Margin" Value="10"/>
</Style>

Platform Considerations

While the Entry control provides a consistent experience across platforms, minor visual differences may exist due to native control implementations. .NET MAUI abstracts these differences, but understanding them can be helpful for advanced customization.

Next Steps

Explore other input controls like Editor for multi-line text entry or SearchBar for search functionality.