当我从 EditText 中删除所有内容时,MvvmCross 不更新值
MvvmCross not updating value when I remove all content from the EditText
我正在使用 Xamarin 和 MvvmCross 为 Android 开发应用程序。
在我的 axml 中,我定义了一个 EditText
:
<EditText
local:MvxBind="Text PorcentagemDesconto"
android:inputType="number"
android:layout_width="1dp"
android:layout_weight="0.3"
android:layout_height="wrap_content"
android:hint="%" />
PorcentagemDesconto 字段是这样声明的:
private decimal? _porcentagemDesconto;
public decimal? PorcentagemDesconto
{
get { return _porcentagemDesconto; }
set
{
_porcentagemDesconto = value;
}
}
假设我在 EditText 上插入值 256。然后,我在 属性 的 set 方法上插入一个断点,然后, 删除所有数字 (按退格键 3 次),断点只会被击中 2次,让私有变量有一个不需要的值2.
对此有一些解决方法,还是我做错了什么?
这是因为值转换失败。如果您连接了调试器,您将在调试输出中看到类似这样的内容:
MvxBind:Error: 47,35 SetValue failed with exception - ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Nullable`1[System.Decimal]'.
03-15 22:11:40.939 I/mono-stdout(25671): MvxBind:Error: 47,35 SetValue failed with exception - ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Nullable`1[System.Decimal]'.
at System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, BindingFlags invokeAttr) [0x00062] in /Users/builder/data/lanes/1196/e79c13cd/source/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:131
at System.Reflection.MonoMethod.ConvertValues (System.Reflection.Binder binder, System.Object[] args, System.Reflection.ParameterInfo[] pinfo, System.Globalization.CultureInfo culture, BindingFlags invokeAttr) [0x0007f] in /Users/builder/data/lanes/1196/e79c13cd/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:335
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00014] in /Users/builder/data/lanes/1196/e79c13cd/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:283
at System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x0006a] in /Users/builder/data/lanes/1196/e79c13cd/source/mono/mcs/class/corlib/System.Reflection/MonoProperty.cs:445
at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) [0x00000] in /Users/builder/data/lanes/1196/e79c13cd/source/mono/mcs/class/corlib/System.Reflection/PropertyInfo.cs:111
邮件的重要部分是
Object of type 'System.String' cannot be converted to type 'System.Nullable`1[System.Decimal]'.
您可以通过在核心库中使用值转换器来解决这个问题
public class NullableValueConverter : MvxValueConverter
{
public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (string.IsNullOrEmpty(value?.ToString()))
return null;
return value;
}
}
并像
一样绑定你的值
<EditText
local:MvxBind="Text PorcentagemDesconto, Converter=Nullable"
android:inputType="number"
android:layout_width="1dp"
android:layout_weight="0.3"
android:layout_height="wrap_content"
android:hint="%" />
我正在使用 Xamarin 和 MvvmCross 为 Android 开发应用程序。
在我的 axml 中,我定义了一个 EditText
:
<EditText
local:MvxBind="Text PorcentagemDesconto"
android:inputType="number"
android:layout_width="1dp"
android:layout_weight="0.3"
android:layout_height="wrap_content"
android:hint="%" />
PorcentagemDesconto 字段是这样声明的:
private decimal? _porcentagemDesconto;
public decimal? PorcentagemDesconto
{
get { return _porcentagemDesconto; }
set
{
_porcentagemDesconto = value;
}
}
假设我在 EditText 上插入值 256。然后,我在 属性 的 set 方法上插入一个断点,然后, 删除所有数字 (按退格键 3 次),断点只会被击中 2次,让私有变量有一个不需要的值2.
对此有一些解决方法,还是我做错了什么?
这是因为值转换失败。如果您连接了调试器,您将在调试输出中看到类似这样的内容:
MvxBind:Error: 47,35 SetValue failed with exception - ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Nullable`1[System.Decimal]'.
03-15 22:11:40.939 I/mono-stdout(25671): MvxBind:Error: 47,35 SetValue failed with exception - ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Nullable`1[System.Decimal]'.
at System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, BindingFlags invokeAttr) [0x00062] in /Users/builder/data/lanes/1196/e79c13cd/source/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:131
at System.Reflection.MonoMethod.ConvertValues (System.Reflection.Binder binder, System.Object[] args, System.Reflection.ParameterInfo[] pinfo, System.Globalization.CultureInfo culture, BindingFlags invokeAttr) [0x0007f] in /Users/builder/data/lanes/1196/e79c13cd/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:335
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00014] in /Users/builder/data/lanes/1196/e79c13cd/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:283
at System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x0006a] in /Users/builder/data/lanes/1196/e79c13cd/source/mono/mcs/class/corlib/System.Reflection/MonoProperty.cs:445
at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) [0x00000] in /Users/builder/data/lanes/1196/e79c13cd/source/mono/mcs/class/corlib/System.Reflection/PropertyInfo.cs:111
邮件的重要部分是
Object of type 'System.String' cannot be converted to type 'System.Nullable`1[System.Decimal]'.
您可以通过在核心库中使用值转换器来解决这个问题
public class NullableValueConverter : MvxValueConverter
{
public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (string.IsNullOrEmpty(value?.ToString()))
return null;
return value;
}
}
并像
一样绑定你的值<EditText
local:MvxBind="Text PorcentagemDesconto, Converter=Nullable"
android:inputType="number"
android:layout_width="1dp"
android:layout_weight="0.3"
android:layout_height="wrap_content"
android:hint="%" />