防止数字输入中的格式异常

huangapple go评论55阅读模式
英文:

Preventing Format Exception in Numeric Entry

问题

在我的.NET MAUI应用程序中,我在视图中使用一个Entry,并使用以下设置:

<Entry Text="{Binding EntryInput}" Placeholder="{Binding PlaceholderString}" Keyboard="{Binding KeyboardSetting}" Margin="5, 0, 5, 15" x:Name="entryControl"/>

根据输入的类型,我使用KeyboardSetting Numeric 或默认值。BindableProperty如下所示:

public static readonly BindableProperty EntryInputProperty = BindableProperty.Create(nameof(EntryInput), typeof(string), typeof(MyInputView), default(string), BindingMode.TwoWay);
...
public string EntryInput
{
    get => (string)GetValue(EntryInputProperty);
    set => SetValue(EntryInputProperty, value);
}

当视图加载时,我想清除Entry的文本并将其设置为string.Empty(而不是默认值"0",即使对于Numeric Entry也是如此)。当我使用KeyboardSetting "Numeric" 时,我遇到了一个 System.FormatException: 'The input string '' was not in a correct format.'

有办法可以预防这种情况并且仍然保持Entry为空(不是"0")吗?当用户从Entry中删除所有文本时也会抛出异常。

在我的情况下,我会说这也会导致UI在加载时变得缓慢。是否有解决方案?

==== 异常的堆栈跟踪 ====

   at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, ReadOnlySpan`1 value, TypeCode type)
   at System.Number.ParseDouble(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)
   at System.Double.Parse(String s, IFormatProvider provider)
   at System.Convert.ToDouble(String value, IFormatProvider provider)
   at System.String.System.IConvertible.ToDouble(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at Microsoft.Maui.Controls.BindingExpression.TryConvert(Object&amp; value, BindableProperty targetProperty, Type convertTo, Boolean toTarget) in D:\a\_work\s\src\Controls\src\Core\BindingExpression.cs:line 464
英文:

In my .NET MAUI App I use an Entry in a View with the following setting:

&lt;Entry Text=&quot;{Binding EntryInput}&quot; Placeholder=&quot;{Binding PlaceholderString}&quot; Keyboard=&quot;{Binding KeyboardSetting}&quot; Margin=&quot;5, 0, 5, 15&quot; x:Name=&quot;entryControl&quot;/&gt;

Depending on the type of input, I use KeyboardSetting Numeric or default. The BindableProperty is as follows:

public static readonly BindableProperty EntryInputProperty = BindableProperty.Create(nameof(EntryInput), typeof(string), typeof(MyInputView), default(string), BindingMode.TwoWay);
...
public string EntryInput
{
	get =&gt; (string)GetValue(EntryInputProperty);
	set =&gt; SetValue(EntryInputProperty, value);
}

When the View is loaded, I want to clear the Text of the Entry and set it to string.Empty (not to the default value "0" also for Numeric Entry). When I use KeyboardSetting "Numeric", I get a System.FormatException: 'The input string '' was not in a correct format.'

Can I prevent this in a way and still have the Entry really empty (not "0")?
Also when a user deletes all the text from the Entry the exception is thrown.

In my case, I would say it also make the UI slow when the UI is loaded.

Are there solutions for that?

==== Stacktrace of the Exception ====

   at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, ReadOnlySpan`1 value, TypeCode type)
   at System.Number.ParseDouble(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)
   at System.Double.Parse(String s, IFormatProvider provider)
   at System.Convert.ToDouble(String value, IFormatProvider provider)
   at System.String.System.IConvertible.ToDouble(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at Microsoft.Maui.Controls.BindingExpression.TryConvert(Object&amp; value, BindableProperty targetProperty, Type convertTo, Boolean toTarget) in D:\a\_work\s\src\Controls\src\Core\BindingExpression.cs:line 464

答案1

得分: 0

根据@Jianwei Sun - MSFT的评论,当我使用Numeric输入时,我尝试将Entry.Text设置为null,而不是设置为string.Empty。非常感谢这个提示!

英文:

As @Jianwei Sun - MSFT commented, I tried it with setting Entry.Text = null instead of setting it to string.Empty when I have a Numeric Entry. Great and thanks for this hint!

huangapple
  • 本文由 发表于 2023年5月14日 01:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定