לקריאה באנגלית

שתף באמצעות

Xamarin.Forms Editor Tutorial

Respond to text changes

  1. In MainPage.xaml, modify the Editor declaration so that it sets a handler for the TextChanged and Completed events:

    XAML
    <Editor Placeholder="Enter multi-line text here"
            HeightRequest="200"
            TextChanged="OnEditorTextChanged"
            Completed="OnEditorCompleted" />
    

    This code sets the TextChanged event to an event handler named OnEditorTextChanged, and the Completed event to an event handler named OnEditorCompleted. Both event handlers will be created in the next step.

  2. 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 OnEditorTextChanged and OnEditorCompleted event handlers to the class:

    C#‎
    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 Editor changes, the OnEditorTextChanged method executes. The sender argument is the Editor object responsible for firing the TextChanged event, and can be used to access the Editor object. The TextChangedEventArgs argument provides the old and new text values, from before and after the text change.

    When the editing is completed, the OnEditorCompleted method executes. This is achieved by unfocussing the Editor, or additionally by pressing the "Done" button on iOS. The sender argument is the Editor object responsible for firing the TextChanged event, and can be used to access the Editor object.

    חשוב

    Any text entered into an Editor is stored in the Text property.

  3. 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:

    Screenshot of Editor containing text, on iOS and Android

    Set breakpoints in the two event handlers, enter text into the Editor, and observe the TextChanged event firing. Unfocus the Editor to observe the Completed event firing.

    For more information about Editor events, see Events and interactivity in the Xamarin.Forms Editor guide.