Xamarin.Forms Editor 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
Editorin XAML. - Respond to the text in the
Editorchanging. - Customize the behavior of the
Editor.
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 Editor. 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 editor
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 EditorTutorial.
Important
The C# and XAML snippets in this tutorial requires that the solution is named EditorTutorial. 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 EditorTutorial 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="EditorTutorial.MainPage"> <StackLayout Margin="20,35,20,20"> <Editor Placeholder="Enter multi-line text here" HeightRequest="200" /> </StackLayout> </ContentPage>This code declaratively defines the user interface for the page, which consists of an
Editorin aStackLayout. TheEditor.Placeholderproperty specifies the placeholder text that's shown when theEditoris first displayed. In addition, theHeightRequestproperty specifies the height of theEditorin device-independent units.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:
Note
While Android indicates the height of the
Editor, iOS does not.In Visual Studio, stop the application.
Respond to text changes
In MainPage.xaml, modify the
Editordeclaration so that it sets a handler for theTextChangedandCompletedevents:<Editor Placeholder="Enter multi-line text here" HeightRequest="200" TextChanged="OnEditorTextChanged" Completed="OnEditorCompleted" />This code sets the
TextChangedevent to an event handler namedOnEditorTextChanged, and theCompletedevent to an event handler namedOnEditorCompleted. Both event handlers will be created in the next step.In Solution Explorer, in the EditorTutorial project, expand MainPage.xaml and double-click MainPage.xaml.cs to open it. Then, in MainPage.xaml.cs, add the
OnEditorTextChangedandOnEditorCompletedevent handlers to the class:void OnEditorTextChanged(object sender, TextChangedEventArgs e) { string oldText = e.OldTextValue; string newText = e.NewTextValue; } void OnEditorCompleted(object sender, EventArgs e) { string text = ((Editor)sender).Text; }When the text in the
Editorchanges, theOnEditorTextChangedmethod executes. Thesenderargument is theEditorobject responsible for firing theTextChangedevent, and can be used to access theEditorobject. TheTextChangedEventArgsargument provides the old and new text values, from before and after the text change.When the editing is completed, the
OnEditorCompletedmethod executes. This is achieved by unfocussing theEditor, or additionally by pressing the "Done" button on iOS. Thesenderargument is theEditorobject responsible for firing theTextChangedevent, and can be used to access theEditorobject.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, enter text into the
Editor, and observe theTextChangedevent firing. Unfocus theEditorto observe theCompletedevent firing.For more information about
Editorevents, see Events and interactivity in the Xamarin.Forms Editor guide.
Customize behavior
In MainPage.xaml, modify the
Editordeclaration to customize its behavior:<Editor Placeholder="Enter multi-line text here" AutoSize="TextChanges" MaxLength="200" IsSpellCheckEnabled="false" IsTextPredictionEnabled="false" />This code sets properties that customize the behavior of the
Editor. TheAutoSizeproperty is set toTextChanges, which indicates that the height of theEditorwill increase when its filled with text, and decrease as text is removed. TheMaxLengthproperty limits the input length that's permitted for theEditor. In addition, theIsSpellCheckEnabledproperty is set tofalseto disable spell checking, while theIsTextPredictionEnabledproperty is set tofalseto disable text prediction and automatic text prediction.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
Editorand observe that the height of theEditorincreases as it fills with text, and decreases as text is removed, and that the maximum number of characters that can be entered is 200:In Visual Studio, stop the application.
For more information about customizing
Editorbehavior, see the Xamarin.Forms Editor guide.
Congratulations!
Congratulations on completing this tutorial, where you learned how to:
- Create a Xamarin.Forms
Editorin XAML. - Respond to the text in the
Editorchanging. - Customize the behavior of the
Editor.
Next steps
To learn more about the basics of creating mobile applications with Xamarin.Forms, continue to the Image 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


