在编辑器 Xamarin Forms 中设置 CursorPosition

Jun*_*min 6 c# xaml custom-renderer xamarin xamarin.forms

我们如何CursorPosition设置Editor. 我们可以设置CursorPostion进去Entry,但是怎么设置呢Editor?我知道我们可以使用 Xamarin Forms 中的自定义渲染器来做到这一点,但是如何实现呢?

Jun*_*min 4

我们不能像设置一样CursorPosition设置。我们需要使用自定义渲染器EditorEntry

使用自定义渲染器可以在 Xamarin Forms 中实现大多数本机函数和实现。

在 Xamarin Forms 中设置光标位置Editor。对于像我这样的初学者来说,这个答案可能有点长,所以请耐心等待。

在您的共享项目中添加一个类EditorExtended.cs

public class EditorExtended : Editor
{
}
Run Code Online (Sandbox Code Playgroud)

在您的XAML页面中添加命名空间以供参考

<xmlns:Local="clr-namespace:ApplicationName.FolderNameThatContainsEditorExtended class">

<!-- If EditorExtended.cs is in "Controls" Folder-->
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ...
             xmlns:Local="clr-namespace:MyApplication.Controls">
            
             ...

    <StackLayout HorizontalOptions="Center">             
        <Local:EditorExtended x:Name="CustomEditor"></Local:EditorExtended>
    </StackLayout>
Run Code Online (Sandbox Code Playgroud)
  • 现在在您的Android 项目中添加自定义渲染器。创建一个文件夹并添加EditorRendererExtended.cs
[assembly: ExportRenderer(typeof(EditorExtended), typeof(EditorRendererExtended))]
namespace MyApplication.Droid.PlatformSpecific.ExtendedControls
{
    public class EditorRendererExtended : EditorRenderer
    {
        public EditorRendererExtended(Context context) : base(context)
        {
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (Control != null)
            {
                Control.RequestFocus();                
                Control.SetSelection(Control.Text.Length);                
            }
        }       
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 同样,在UWP 项目中创建一个文件夹并在平台特定代码中添加自定义渲染器。
[assembly: ExportRenderer(typeof(EditorExtended), typeof(EditorRendererExtended))]
namespace MyApplication.UWP.PlatformSpecific.ExtendedControls
{
    public class EditorRendererExtended: EditorRenderer
    {
        public EditorRendererExtended()
        {
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (Control != null)
            {
                Control.Focus(Windows.UI.Xaml.FocusState.Pointer);
                Control.SelectionStart = Control.Text.Length;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 我还没有在iOS中测试过,但是方法是类似的。只需在 iOS 的 Platform Specific 文件夹中添加EditorRendererExtended.cs类即可。该代码未经测试,如果您知道解决方案,可以自由编辑答案。这是我已经实现但未经测试的代码。
[assembly:ExportRenderer(typeof(EditorExtended), typeof(EditorRendererExtended))]
namespace MyApplication.iOS.PlatformSpecific.ExtendedControls
{
    public class EditorRendererExtended : EditorRenderer
    {
        public EditorRendererExtended()
        {
        }

        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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不要忘记在您想要定位的所有平台特定代码中包含以下语句,否则它将无法工作

[assembly: ExportRenderer(typeof(EditorExtended), typeof(EditorRendererExtended))]
Run Code Online (Sandbox Code Playgroud)

EditorRendererExtended对于每个平台都是不同的,您可以更改其名称,例如EditorRendererExtendedAndroidEditorRendererExtendedUWP,以便更好地理解。我只是将它们命名为相似的,因为我发现不需要以不同的方式命名它们,并且会使其不必要地冗长。