Xamarin 编辑器行为类似于未聚焦事件的Complte事件
在点击键盘上的“完成”后,我试图在编辑器中保存一些值,因为我为编辑器使用了Xamarin 编辑器行为类似于未聚焦事件的Complte事件,xamarin,xamarin.forms,editor,Xamarin,Xamarin.forms,Editor,在点击键盘上的“完成”后,我试图在编辑器中保存一些值,因为我为编辑器使用了Completed事件,但这是在点击视图中的任何位置时调用的,如Unfocused事件。如何避免这种情况 <controls:CustomEditor Keyboard="Default" ReturnKeyType="Next" TextChanged="Comment_Changed"
Completed
事件,但这是在点击视图中的任何位置时调用的,如Unfocused
事件。如何避免这种情况
<controls:CustomEditor Keyboard="Default"
ReturnKeyType="Next"
TextChanged="Comment_Changed"
Completed="OnDoneClicked"
VerticalOptions="StartAndExpand"
HorizontalOptions="FillAndExpand"
Text="{Binding QuestionComment}">
<controls:CustomEditor.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double"
iOS="12"
Android="12"
WinPhone="30" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double"
iOS="13"
Android="13"
WinPhone="40" />
</OnIdiom.Tablet>
</OnIdiom>
</controls:CustomEditor.FontSize>
</controls:CustomEditor>
活动内容如下:
public const string ReturnKeyPropertyName = "ReturnKeyType";
public CustomEditor() { }
public static readonly BindableProperty ReturnKeyTypeProperty = BindableProperty.Create(
propertyName: ReturnKeyPropertyName,
returnType: typeof(ReturnKeyTypes),
declaringType: typeof(CustomEditor),
defaultValue: ReturnKeyTypes.Done );
public ReturnKeyTypes ReturnKeyType
{
get { return (ReturnKeyTypes)GetValue(ReturnKeyTypeProperty); }
set { SetValue(ReturnKeyTypeProperty, value); }
}
public enum ReturnKeyTypes : int
{
Default,
Go,
Google,
Join,
Next,
Route,
Search,
Send,
Yahoo,
Done,
EmergencyCall,
Continue
}
private async void OnDoneClicked(object sender, EventArgs e)
{
//some logic
}
请参阅,这是预期的行为
iOS(取消编辑器焦点或按“完成”将触发事件)。Android/Windows Phone(取消编辑器焦点会触发事件)
我检查了一些messenger应用程序(iOS),它们的键盘上根本没有done
按钮,除了输入控件之外,它们还提供了一个发送按钮
根据您试图实现的目标,我会完全放弃键盘上的done
按钮,而依赖于UI上的某种视图,这正是用于此目的的。不管怎么说,这是依赖于平台的行为,安卓根本不支持它。为了摆脱“完成”按钮,您必须实现一个自定义渲染器,并将本机控件的InputAccessoryView
设置为null
(请参阅)
如果确实需要发送按钮,可以使用自定义渲染器创建Editor
的派生版本,该版本创建InputAccessoryView
并在按下InputAccessoryView
时在自定义编辑器上引发SendPressed
事件。但请记住,这对于Android是不可能的
[assembly: ExportRenderer(typeof(CustomEditor), typeof(CustomEditorRenderer))]
namespace ProjectName.iOS
{
public class CustomEditorRenderer : EditorRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
this.Control.InputAccessoryView = null;
}
}
}