Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] c# – Xamarin Editor Control CursorPosition on Android

Posted on November 14, 2022

Solution 1 :

Editor do not support CursorPosition Changed event. You could create a custom control EditText. EditText provides OnSelectionChanged event. And add this custom EditText in EditorRenderer.

MyEditor.cs: Create Editor custom control. And binding the OnSelectionChanged event.

public class MyEditor : Editor
{
    public static readonly BindableProperty SelectionChangedProperty =
    BindableProperty.Create("SelectionChanged", typeof(EventHandler), typeof(MyEditor), null);

    public event EventHandler SelectionChanged;

   public void SelectionChange(int preIndex,int currentIndex)
    {
        EventHandler eventHandler = this.SelectionChanged;
        SelectionEventArgs selectionEventArgs = new SelectionEventArgs() { lastPos = preIndex, curPos = currentIndex };
        eventHandler?.Invoke((object)this, selectionEventArgs);
    }

   public class SelectionEventArgs : EventArgs
    {
       public int lastPos { get; set; }
        public int curPos { get; set; }
    }
}

EditorRenderer.cs:

[assembly: ExportRenderer(typeof(MyEditor), typeof(XamarinDemo.Droid.EditorRenderer))]
namespace XamarinDemo.Droid
{
class EditorRenderer : Xamarin.Forms.Platform.Android.EditorRenderer, MyEditText.EditTextSelectChange
{
    Context _context;
    public EditorRenderer(Context context) : base(context)
    {
        _context = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
    {
        base.OnElementChanged(e);

        MyEditText myEditText = new MyEditText(_context);
        myEditText.Text = Element.Text;
        myEditText.setEditTextSelectChange(this);
        SetNativeControl(myEditText);
    }


    public void change(int lastPos, int curPos)
    {
        //Console.WriteLine("lastPos ===" + lastPos + "curPos ====" + curPos);
        ((MyEditor)Element).SelectionChange(lastPos, curPos);
    }

}
}

MyEditText.cs: In Xamarin.Forms Android spefic project, create a EditTextSelectChange for EditorRenderer to inherit.

 class MyEditText :FormsEditText
{

    private int mLastPos = 0;
    private int mCurPos = 0;

    private EditTextSelectChange editTextSelectChange;

    public void setEditTextSelectChange(EditTextSelectChange editTextSelectChange)
    {
        this.editTextSelectChange = editTextSelectChange;
    }

    public MyEditText(Context context):base(context)
    {
    }


    protected override void OnSelectionChanged(int selStart, int selEnd)
    {
        base.OnSelectionChanged(selStart, selEnd);
        if (this.editTextSelectChange != null)
        {
            mCurPos = selEnd;
            editTextSelectChange.change(mLastPos, mCurPos);
            mLastPos = mCurPos;
        }

    }

    public interface EditTextSelectChange
    {
        void change(int lastPos, int curPos);
    }
}

Usage:

        <local:MyEditor SelectionChanged="SelectionChange" Text="hakssksksssfsvdsv" />

Code Behind:

 public partial class Page13 : ContentPage
{
    public Page13()
    {
        InitializeComponent();
    }

    private void SelectionChange(object sender, EventArgs e)
    {
        Console.WriteLine("lastpos =="+ ((SelectionEventArgs)e).lastPos +"curpos =="+ ((SelectionEventArgs)e).curPos);
    }
}

Screenshot:

enter image description here

Problem :

I’m currently working on a project that requires alot of text manipulation and i’m trying to create a function that can insert text at the current CursorPosition. I’m using a Editor as my Text Control and i noticed that Xamarin does not provide an implementation for CursorPosition yet.

READ  [ANSWERED] xaml - xamarin.forms error The name does not exist in the current context
Powered by Inline Related Posts

I found out that the Xamarin developers have this on their backlog but I can’t wait for this as I am on a deadline.

I made my own CustomRenderer for the Editor control and i got CursorPosition working for UWP. When i tried to do the same thing for Android however I noticed that I need to use an the event EventHandler SelectionChanged; in the IFormsEditText interface that is not available to me as they marked the interface internal.

Little snippit of code to display progress:

internal class EditorRenderer : Xamarin.Forms.Platform.Android.EditorRenderer
{
    bool _cursorPositionChangePending;
    bool _nativeSelectionIsUpdating;

    IElementController ElementController => Element as IElementController;

    public EditorRenderer(Context context) : base(context) { }

    private Editor _editor;

    protected override void OnElementChanged(ElementChangedEventArgs<XamarinForms.Editor> e)
    {
        base.OnElementChanged(e);

        _editor = (Editor)Element;

        _cursorPositionChangePending = Element.IsSet(Editor.CursorPositionProperty);

        // I wanted to do something along the lines of this
        //if (Control is IFormsEditText editText)
        //{
        //    editText.SelectionChanged += SelectionChanged;
        //}

        if (_cursorPositionChangePending)
            UpdateCursorSelection();
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (e.PropertyName == Editor.CursorPositionProperty.PropertyName)
        {
            UpdateCursorSelection();
        }
    }
    void SelectionChanged(object sender, XamarinFormsDroid.SelectionChangedEventArgs e)
    {
        if (_nativeSelectionIsUpdating || Control == null || Element == null)
            return;

        int cursorPosition = _editor.CursorPosition;
        int selectionStart = EditText.SelectionStart;

        if (!_cursorPositionChangePending)
        {
            var start = cursorPosition;

            if (selectionStart != start)
                SetCursorPositionFromRenderer(selectionStart);
        }

        if (!_selectionLengthChangePending)
        {
            int elementSelectionLength = Math.Min(EditText.Text.Length - cursorPosition, _editor.SelectionLength);

            var controlSelectionLength = EditText.SelectionEnd - selectionStart;
            if (controlSelectionLength != elementSelectionLength)
                SetSelectionLengthFromRenderer(controlSelectionLength);
        }
    }

Has anyone tried making their own renderer for android with a working implementation for CursorPosition ? Or does anyone have any idea on how this could be realized ? Please let me know.

Comments

Comment posted by Wendy Zang – MSFT

Do you mean you want to set the cursor position in a editor control?

Comment posted by Cinedine

No, I want to know when the cursor has moved in the editor control (for Android). The event i need to use is internal.

READ  [ANSWERED] android - Enable view when RadioButton checked
Powered by Inline Related Posts

Comment posted by Cinedine

But won’t this get rid of all the behavior in the base control ? I got the selection event working like this but for example the TextChanged event wont’ fire if i overwrite this original control.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress