Cursor keep jumping to the left (to the start)

80 views Asked by At

I'm trying to add a different behaviors and converters. but no matter what I'm doing, the cursor keeps going back to the start.

For example, I'm trying to use behavior to prevent more than 1 whitespace in a row.

So when the user typing more than 1 space (in a row), it should remove the 2nd space and leave only 1 (text.Replace(" ", " ")) and the cursor should stay at it's position. But instead, the cursor jumping back to the start. Its doing it also with different behaviors.

How can I make the cursor to stay at its position so if the user type "This is nice". it will be "This is nice" (and not "is niceThis ").

I hope I explained myself correctly. Thank you so much.

I just created a fresh app (still doing the same) This is my code

xaml:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TestProj.Behaviors"
             x:Class="TestProj.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Entry Placeholder="Hello">
                <Entry.Behaviors>
                    <local:WhiteSpaceBehavior />
                </Entry.Behaviors>
            </Entry>

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

Behavior Class:

namespace TestProj.Behaviors {
    public class WhiteSpaceBehavior : Behavior<Entry>
    {
        protected override void OnAttachedTo(Entry entry)
        {
            base.OnAttachedTo(entry);
            entry.TextChanged += OnEntryTextChanged;
        }

        protected override void OnDetachingFrom(Entry entry)
        {
            entry.TextChanged -= OnEntryTextChanged;
            base.OnDetachingFrom(entry);
        }

        private void OnEntryTextChanged(object sender, TextChangedEventArgs e)
        {
            var entry = (Entry)sender;
            var newText = e.NewTextValue;

            if (ContainsConsecutiveWhiteSpaces(newText))
            {
                newText = RemoveConsecutiveWhiteSpaces(newText);
                entry.Text = newText;
            }
        }

        private bool ContainsConsecutiveWhiteSpaces(string text)
        {
            for (int i = 0; i < text.Length - 1; i++)
            {
                if (char.IsWhiteSpace(text[i]) && char.IsWhiteSpace(text[i + 1]))
                {
                    return true;
                }
            }
            return false;
        }

        private string RemoveConsecutiveWhiteSpaces(string text)
        {
            var newText = text.Replace("  ", " ");
            if (newText.Contains("  "))
            {
                newText = RemoveConsecutiveWhiteSpaces(newText);
            }
            return newText;
        }
    }
 }
1

There are 1 answers

5
Alexandar May - MSFT On

I can reproduce the issue when using .NET 7. The cursor keeps jumping to the start point of the Entry when you entered more than one whitespaces. Fortunately, it's being fixed on .NET 8.

To fix it, you can reset the CursorPosition after removing the extra whitespaces. And the cursor should stay at its position as expected.

private void OnEntryTextChanged(object sender, TextChangedEventArgs e)
{
      var entry = (Entry)sender;
      var newText = e.NewTextValue;

      if (ContainsConsecutiveWhiteSpaces(newText))
      {
            newText = RemoveConsecutiveWhiteSpaces(newText);
            entry.Text = newText;
      }

      //key is here
      entry.CursorPosition = entry.Text.Length;
}