Set CursorPosition in Editor Xamarin Forms

眉间皱痕 提交于 2021-02-11 18:21:48

问题


How can we set CursorPosition in Editor. We can set CursorPostion in Entry, but how to set it in Editor? I know we can do it using Custom Renderer in Xamarin Forms, but how to implement it?


回答1:


We cannot set CursorPosition in Editor as we set it in Entry. We need to use Custom Renderer.

Using Custom Renderer makes most of the native functions and implementations possible in Xamarin Forms.

To set Cursor Position in Editor in Xamarin Forms. This answer may be a bit lenghthier for beginners like me so please bear with me.

In your Shared Project add a Class EditorExtended.cs

public class EditorExtended : Editor
{
}

In your XAML Page add namespace for reference

<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>
  • Now in your Android Project add Custom Renderer. Make a folder and add 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);                
            }
        }       
    }
}
  • Similarly in UWP Project Make a folder and add Custom Renderer in Platform Specific code.
[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;
            }
        }
    }
}
  • I haven't tested in iOS, but the method will be similar. Just add EditorRendererExtended.cs class in Platform Specific folder in iOS. This code is not tested, you are free to edit the answer if you know the solution. Here is the code I have implemented, but not tested.
[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);
            }
        }
    }
}

Do not forget to include the below statement in all the Platform Specific code that you want to target, otherwise it won't work

[assembly: ExportRenderer(typeof(EditorExtended), typeof(EditorRendererExtended))]

EditorRendererExtended is different for each platform, you can change its name like EditorRendererExtendedAndroid or EditorRendererExtendedUWP for better understanding. I have just named them similar as I find naming them differently is not needed and makes it unnecessarily lengthy.



来源:https://stackoverflow.com/questions/62623756/set-cursorposition-in-editor-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!