我试图将输入字段中的值限制为某个值。我尝试了以下方法。当用户输入任何大于最大限制的数字时,字段设置为 0。
但是,当用户按退格键或尝试输入任何新数字时,应用程序就会崩溃。
查看型号:
private int? getHeight = null;
public int? GetHeight
{
get { return getHeight; }
set
{
if (!getHeight.HasValue || (!getHeight.Equals(value) && value.Value <= 250))
{
getHeight = value;
}
else
{
getHeight = 0;
}
OnPropertyChanged();
}
}
看法:
heightEntry = new Entry
{
Keyboard = Keyboard.Numeric
};
heightEntry.SetBinding(Entry.TextProperty, nameof(MyViewModel.GetHeight), converter: new NullableIntConverter());
heightEntry.SetBinding(Entry.IsEnabledProperty, nameof(MyViewModel.UiElementsEnabled));
optionsGrid.Children.Add(heightEntry, 1, 1);
应用程序因 OnPropertyChanged() 而崩溃,即当字段设置为零时,OnPropertyChanged() 被触发并且不允许再次输入。
也许尝试这样的事情,只需在调用“onpropertychange”之前确保该值不为空。
private int? getHeight = null;
public int? GetHeight
{
get { return getHeight; }
set
{
if (!getHeight.HasValue || (!getHeight.Equals(value) && value.Value <= 250))
{
getHeight = value;
}
else
{
getHeight = 0;
value = 0; //override the value
}
// validate against our value for the property change.
if (value.HasValue && value != null)
{
OnPropertyChanged();
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句