admin管理员组

文章数量:1401592

I had a fully working reactive currency Entry in my .NET Maui app...

The entry started with "R$ 0,00"

When you type 5, it goes to "R$ 0,05", press 3, it goes to "R$0,53" and so on...

After upgrading my NET to 9.0, it works thil "R$ 159,00" when I press "0" again I get this error:

Java.Lang.IllegalArgumentException: 'end should be < than charSequence length'

It was working before, now it doesn't. Maybe this error because of "." in "R$ 1.590,00"

Code:

using System.Globalization;

namespace Mobile;
    public class EntryValores : Entry
{
    private bool executingTextChanged = false;

    public EntryValores()
    {
       // HorizontalTextAlignment = TextAlignment.End;
        TextChanged += EntryValores_OnTextChanged;
    }

    protected void EntryValores_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        if (executingTextChanged) return;
        executingTextChanged = true;

        // Formatar valores
        decimal valor;
        string valorTexto = "0";

        // Retornar apenas os dígitos
        foreach (char c in Text)
        {
            if (char.IsDigit(c))
            {
                valorTexto += c;
            }
        }

        // Se for Nulo, então colocar 0
        if (string.IsNullOrEmpty(valorTexto))
        {
            valorTexto = "0";
        }

        valor = decimal.Parse(valorTexto) / 100;

        // Verificar limites
        if (valor > 999999999999m || valor < -999999999999m)
        {
            // Se o valor for maior que 100 trilhões ou menor que -100 trilhões, cancelar a entrada
            Text = e.OldTextValue;
        }
        else
        {
            // Colocar o valor formatado
            Text = valor.ToString("C", CultureInfo.CurrentCulture);
        }

        executingTextChanged = false;
    }
}

I had a fully working reactive currency Entry in my .NET Maui app...

The entry started with "R$ 0,00"

When you type 5, it goes to "R$ 0,05", press 3, it goes to "R$0,53" and so on...

After upgrading my NET to 9.0, it works thil "R$ 159,00" when I press "0" again I get this error:

Java.Lang.IllegalArgumentException: 'end should be < than charSequence length'

It was working before, now it doesn't. Maybe this error because of "." in "R$ 1.590,00"

Code:

using System.Globalization;

namespace Mobile;
    public class EntryValores : Entry
{
    private bool executingTextChanged = false;

    public EntryValores()
    {
       // HorizontalTextAlignment = TextAlignment.End;
        TextChanged += EntryValores_OnTextChanged;
    }

    protected void EntryValores_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        if (executingTextChanged) return;
        executingTextChanged = true;

        // Formatar valores
        decimal valor;
        string valorTexto = "0";

        // Retornar apenas os dígitos
        foreach (char c in Text)
        {
            if (char.IsDigit(c))
            {
                valorTexto += c;
            }
        }

        // Se for Nulo, então colocar 0
        if (string.IsNullOrEmpty(valorTexto))
        {
            valorTexto = "0";
        }

        valor = decimal.Parse(valorTexto) / 100;

        // Verificar limites
        if (valor > 999999999999m || valor < -999999999999m)
        {
            // Se o valor for maior que 100 trilhões ou menor que -100 trilhões, cancelar a entrada
            Text = e.OldTextValue;
        }
        else
        {
            // Colocar o valor formatado
            Text = valor.ToString("C", CultureInfo.CurrentCulture);
        }

        executingTextChanged = false;
    }
}
Share Improve this question edited Mar 25 at 13:09 Heitor Badotti asked Mar 24 at 19:35 Heitor BadottiHeitor Badotti 3081 silver badge13 bronze badges 3
  • It does not looks like there a question to answer... (It is possible that "solution" section is an answer ... which should than be posted as answer... ). – Alexei Levenkov Commented Mar 24 at 20:49
  • Mostly, we do expect you to edit your own question in place as you have more information. But if you solve your own problem, rather than editing in-place that is a situation where you would add the solution as a separate Answer. – Joel Coehoorn Commented Mar 24 at 21:13
  • decimal.Parse(valorTexto) - I would recommend including the culture when parsing. I'm not sure what the default behavior is, but specifying the culture will make it more explicit. – JonasH Commented Mar 25 at 8:30
Add a comment  | 

1 Answer 1

Reset to default 1

SOLUTION

I found a thread in github which they say to add this line of code in TextChanged event

protected void EntryValores_OnTextChanged(object sender, TextChangedEventArgs e)
{

 // This part was added to fix the BUG 
#if ANDROID
    var handler = Handler as Microsoft.Maui.Handlers.EntryHandler;
    var editText = handler?.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText;
    if (editText != null)
    {
        editText.EmojiCompatEnabled = false;
        editText.SetTextKeepState(Text);
    }
#endif
// code...
}

It worked fine after that...

本文标签: cCurrency input problem 39end should be lt than charSequence length39 after NET updateStack Overflow