英文:
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& 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:
<Entry Text="{Binding EntryInput}" Placeholder="{Binding PlaceholderString}" Keyboard="{Binding KeyboardSetting}" Margin="5, 0, 5, 15" x:Name="entryControl"/>
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 => (string)GetValue(EntryInputProperty);
set => 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& 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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论