在编辑器中,如何复制条目的"已完成"事件行为?



当按下返回键时,条目的已完成事件被触发。我在编辑器中也需要同样的行为。有人能给我举一些如何做到这一点的例子吗?

编辑器的默认实现仅在

iOS (Unfocusing the editor or pressing "Done" triggers the event).
Android / Windows Phone (Unfocusing the Editor triggers the event).

是的,您可以使用`Editor的事件Completed来实现这一点。

Editor暴露两个事件:

  • TextChanged–当编辑器中的文本发生更改时引发。提供更改前后的文本
  • Completed(完成(–当用户按结束输入时引发键盘上的回车键

Completed事件用于对完成与Editor的交互作出反应。当用户通过在键盘上输入回车键(或通过按下UWP上的Tab键(结束字段输入时,Completed会被触发。事件的处理程序是一个通用的事件处理程序,采用发送方和EventArgs:

void EditorCompleted (object sender, EventArgs e)
{
var text = ((Editor)sender).Text; // sender is cast to an Editor to enable reading the `Text` property of the view.
}

已完成的事件可以在代码和XAML中订阅:

在XAML中:

<?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="TextSample.EditorPage"
Title="Editor Demo">
<ContentPage.Content>
<StackLayout Padding="5,10">
<Editor Completed="EditorCompleted" />
</StackLayout>
</ContentPage.Content>
</Contentpage>

和C#:

public partial class EditorPage : ContentPage
{
public EditorPage ()
{
InitializeComponent ();
var layout = new StackLayout { Padding = new Thickness(5,10) };
this.Content = layout;
var editor = new Editor ();
editor.Completed += EditorCompleted;
layout.Children.Add(editor);
}
}

有关详细信息,请查看:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/editor#events-和互动

最新更新