
[英]Xamarin.Forms ListView: Set the highlight color of a tapped item
[英]Set cursor position of the editor control exactly where user tapped in Xamarin.forms
我在 Xamarin.forms 项目工作。 我想将编辑器控件的 cursor position 设置为用户点击的确切位置。 我想在 IOS 中专门实现这一点(最好在 Android 中也有解决方案)。 我浏览了几篇文章,他们建议使用自定义渲染器,但我不知道如何使用。 我已经阅读了以下文章
在编辑器中设置 CursorPosition Xamarin Forms
基于上面的链接,我制作了编辑器控件的自定义渲染器。 自定义渲染中 OnElementPropertyChanged 中的代码将我的 cursor 保留在编辑器控件的起始 position 上,但我不想要这个。 cursor position 应该更改用户点击的位置。 我们需要根据我的用例更新渲染器中的代码。
public class CustomEditor : Editor
{
}
public class CustomEditorRenderer : EditorRenderer
{
public CustomEditorRenderer() { }
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Control != null)
{
// just change this statement to the one that works.
Control.SelectedTextRange = Control.GetTextRange(fromPosition: Control.BeginningOfDocument, toPosition: Control.BeginningOfDocument);
}
}
}
我必须使用 Editor 控件,而不是 Entry 控件,因为我想在多行中输入文本。 编辑器控件没有像 Entry 控件那样的 CursorPosition 属性。
任何人都可以通过自定义渲染器或利用 TextChanged、Focused 事件提出更好的方法吗?
您可以更改渲染器代码以更改 cursor position:
//Change the cursor position to the fifth character
var test = Control.GetPosition(Control.BeginningOfDocument, 5);
Control.SelectedTextRange = Control.GetTextRange(test, test);
如果要把cursor改成用户点击的position,需要获取用户点击的position,转换成对应的索引值。 获取用户点击position可以参考文档:Walkthrough: Using Touch in Xamarin.iOS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.