我正在使用 MvvmCross 将 ViewModel 中的数据绑定到 axml 布局文件中的属性。一旦应用程序完全加载,这些绑定就会正常工作。但是,调试输出日志中有大量“MvxBind:Error”消息,显示“Null 对象无法转换为值类型”。所有这些错误都发生在 android 应用程序显示初始“SplashScreen.axml”视图之前。在那之后,所有绑定都按预期工作。
还值得注意的是,这些错误仅在将值绑定到自定义控件中的属性或绑定通过自定义转换器传递的值时发生。我没有看到任何问题,例如,将字符串绑定到 TextView 中的 Text 属性。
错误的示例日志输出:
MvxBind:Error: 17.76 Problem seen during binding execution for from LocationCurrentMovementViewModel.IsAlarmSignaled to Visibility - problem InvalidCastException: Null object can not be converted to a value type.
07-17 20:05:51.980 I/mono-stdout( 3056): MvxBind:Error: 17.76 Problem seen during binding execution for from LocationCurrentMovementViewModel.IsAlarmSignaled to Visibility - problem InvalidCastException: Null object can not be converted to a value type.
07-17 20:05:51.980 I/mono-stdout( 3056): at System.Convert.ToType (System.Object value, System.Type conversionType, IFormatProvider provider, Boolean try_target_to_type) [0x00000] in <filename unknown>:0
07-17 20:05:51.990 I/mono-stdout( 3056): at System.Convert.ChangeType (System.Object value, System.Type conversionType, IFormatProvider provider) [0x00000] in <filename unknown>:0
07-17 20:05:51.990 I/mono-stdout( 3056): at Cirrious.MvvmCross.Binding.ExtensionMethods.MvxTypeExtensions.MakeSafeValue (System.Type propertyType, System.Object value) [0x00000] in <filename unknown>:0
07-17 20:05:51.990 I/mono-stdout( 3056): at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.MakeSafeValue (System.Object value) [0x00000] in <filename unknown>:0
at System.Convert.ToType (System.Object value, System.Type conversionType, IFormatProvider provider, Boolean try_target_to_type) [0x00000] in <filename unknown>:0
at System.Convert.ChangeType (System.Object value, System.Type conversionType, IFormatProvider provider) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.ExtensionMethods.MvxTypeExtensions.MakeSafeValue (System.Type propertyType, System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.MakeSafeValue (System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue (System.Object value) [0x00000] in <filename unknown>:0
07-17 20:05:51.990 I/mono-stdout( 3056): at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue (System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (Boolean isAvailable, System.Object value) [0x00000] in <filename unknown>:0
[0:] MvxBind:Error: 17.76 Problem seen during binding execution for from LocationCurrentMovementViewModel.IsAlarmSignaled to Visibility - problem InvalidCastException: Null object can not be converted to a value type.
at System.Convert.ToType (System.Object value, System.Type conversionType, IFormatProvider provider, Boolean try_target_to_type) [0x00000] in <filename unknown>:0
at System.Convert.ChangeType (System.Object value, System.Type conversionType, IFormatProvider provider) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.ExtensionMethods.MvxTypeExtensions.MakeSafeValue (System.Type propertyType, System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.MakeSafeValue (System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue (System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (Boolean isAvailable, System.Object value) [0x00000] in <filename unknown>:0
07-17 20:05:51.990 I/mono-stdout( 3056): at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (Boolean isAvailable, System.Object value) [0x00000] in <filename unknown>:0
这是引发该特定错误的绑定:
<FrameLayout
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="317dp"
p1:layout_height="360dp"
p1:id="@+id/MovementAlertOverlay"
p1:layout_marginTop="135dp"
p1:layout_marginLeft="639dp"
p1:background="@drawable/alert_border"
local:MvxBind="Visibility LocationCurrentMovementViewModel.IsAlarmSignaled,Converter=BoolToViewStates"
/>
转换器代码:
public class BoolToViewStatesValueConverter : MvxValueConverter<bool, ViewStates>
{
protected override ViewStates Convert(bool value, System.Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (value)
{
return ViewStates.Visible;
}
return ViewStates.Gone;
}
}
我尝试在转换器中检查“值”以查看它是否为空,然后再返回任何内容。我还尝试将“LocationCurrentMovementViewModel.IsAlarmSignaled”初始化为默认值。这对显示的错误没有影响。
对象“LocationCurrentMovementViewModel”嵌套在与 axml 布局文件关联的 ViewModel 中。它是在运行时动态设置的。但是,我证实这不是问题的原因。我创建了一个临时对象“TempBoolTrue,它只是一个设置为 true 的布尔变量。我用它来替换 axml 布局文件中的“LocationCurrentMovementViewModel.IsAlarmSignaled”。这只会导致类似的错误,用“TempBoolTrue”代替“LocationCurrentMovementViewModel.IsAlarmSignaled”。
对于解决此问题的任何帮助,我将不胜感激。
谢谢!
- - 编辑 - -
我最近开始从事这个项目。因此,最初设置时我不在。有几个问题浮出水面。
- MvvmCross 资源是手动添加的。它们现在是使用 nuget 以正确的版本 3.0.9 添加的。
- 我还发现 SplashScreen.cs 已被删除。SplashScreen.axml 的自定义版本稍后在启动过程中被调用。
- Android 应用程序以不规则的方式启动。以这种方式启动应用程序最初似乎有充分的理由;但是,最终的解决方案可能需要稍作改动。
剩下的部分需要一些时间来整理。但是,您的帖子确实帮助我发现了问题的根源。谢谢斯图尔特!