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
|
1 Answer
Reset to default 1SOLUTION
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
版权声明:本文标题:c# - Currency input problem 'end should be < than charSequence length' after .NET update - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744231449a2596356.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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