Xamarin.Forms Entry Tutorial
Before attempting this tutorial, you should have successfully completed the:
- Build your first Xamarin.Forms app quickstart.
- StackLayout tutorial.
In this tutorial, you learn how to:
- Create a Xamarin.Forms
Entryin XAML. - Respond to the text in the
Entrychanging. - Customize the behavior of the
Entry.
You will use Visual Studio 2019, or Visual Studio for Mac, to create a simple application that demonstrates how to customize the behavior of an Entry. The following screenshots show the final application:
You'll also use XAML Hot Reload for Xamarin.Forms to see UI changes without rebuilding your application.
Create a entry
To complete this tutorial you should have Visual Studio 2019 (latest release), with the Mobile development with .NET workload installed. In addition, you will require a paired Mac to build the tutorial application on iOS. For information about installing the Xamarin platform, see Installing Xamarin. For information about connecting Visual Studio 2019 to a Mac build host, see Pair to Mac for Xamarin.iOS development.
Launch Visual Studio, and create a new blank Xamarin.Forms app named EntryTutorial.
Important
The C# and XAML snippets in this tutorial requires that the solution is named EntryTutorial. Using a different name will result in build errors when you copy code from this tutorial into the solution.
For more information about the .NET Standard library that gets created, see Anatomy of a Xamarin.Forms application in the Xamarin.Forms Quickstart Deep Dive.
In Solution Explorer, in the EntryTutorial project, double-click MainPage.xaml to open it. Then, in MainPage.xaml, remove all of the template code and replace it with the following code:
<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="EntryTutorial.MainPage"> <StackLayout Margin="20,35,20,20"> <Entry Placeholder="Enter text" /> </StackLayout> </ContentPage>This code declaratively defines the user interface for the page, which consists of an
Entryin aStackLayout. TheEntry.Placeholderproperty specifies the placeholder text that's shown when theEntryis first displayed.In the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator:
In Visual Studio, stop the application.
Respond to text changes
In MainPage.xaml, modify the
Entrydeclaration so that it sets a handler for theTextChangedandCompletedevents:<Entry Placeholder="Enter text" TextChanged="OnEntryTextChanged" Completed="OnEntryCompleted" />This code sets the
TextChangedevent to an event handler namedOnEntryTextChanged, and theCompletedevent to an event handler namedOnEntryCompleted. Both event handlers will be created in the next step.In Solution Explorer, in the EntryTutorial project, expand MainPage.xaml and double-click MainPage.xaml.cs to open it. Then, in MainPage.xaml.cs, add the
OnEntryTextChangedandOnEntryCompletedevent handlers to the class:void OnEntryTextChanged(object sender, TextChangedEventArgs e) { string oldText = e.OldTextValue; string newText = e.NewTextValue; } void OnEntryCompleted(object sender, EventArgs e) { string text = ((Entry)sender).Text; }When the text in the
Entrychanges, theOnEntryTextChangedmethod executes. Thesenderargument is theEntryobject responsible for firing theTextChangedevent, and can be used to access theEntryobject. TheTextChangedEventArgsargument provides the old and new text values, from before and after the text change.When you finalize the text in the
Entrywith the return key, theOnEntryCompletedmethod executes. Thesenderargument is theEntryobject responsible for firing theTextChangedevent, and can be used to access theEntryobject.In the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator:
Set breakpoints in the two event handlers and enter text into the
Entryand observe theTextChangedandCompletedevents firing.For more information about
Entryevents, see Events and interactivity in the Xamarin.Forms Entry guide.
Customize behavior
In MainPage.xaml, modify the
Entrydeclaration to customize its behavior:<Entry Placeholder="Enter password" MaxLength="15" IsSpellCheckEnabled="false" IsTextPredictionEnabled="false" IsPassword="true" />This code sets properties that customize the behavior of the
Entry. TheMaxLengthproperty limits the input length that's permitted for theEntry, and theIsSpellCheckEnabledproperty is set tofalseto disable spell checking. Similarly, theIsTextPredictionEnabledproperty is set tofalseto disable text prediction and automatic text prediction. In addition, theIsPasswordproperty ensures that entered characters are masked with a password character (a black circle).Note
For some text entry scenarios, such as entering a password, spell checking and text prediction provides a negative experience and should therefore be disabled.
If the application is still running, save the changes to the file and the application user interface will automatically be updated in your simulator or emulator. Otherwise, in the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator. Enter text into the
Entryand observe that each character is replaced with a password mask character, and that the maximum number of characters that can be entered is 15:In Visual Studio, stop the application.
For more information about customizing
Entrybehavior, see the Xamarin.Forms Entry guide.
Congratulations!
Congratulations on completing this tutorial, where you learned how to:
- Create a Xamarin.Forms
Entryin XAML. - Respond to the text in the
Entrychanging. - Customize the behavior of the
Entry.
Next steps
To learn more about the basics of creating mobile applications with Xamarin.Forms, continue to the Editor tutorial.
Related links
Have an issue with this section? If so, please give us some feedback so we can improve this section.
Povratne informacije
Pošalјite i prikažite povratne informacije za


