android - 当路径上的第一项为空时,与路径的绑定(bind)不起作用

标签 android xamarin.ios xamarin.android mvvmcross

使用 MvvmCross,我试图将 TextView 的 android:visibility 绑定(bind)到类似于“Results.Count”的路径,并设置转换器以将整数转换为可见性(如果不为空且大于零,则 TextView 可见) .

如果 Results 为空,则在应用输出中显示错误:

[MvxBind] 1.34 Problem seen during binding execution for from SearchResult.Count to Visibility - problem InvalidCastException: Null object can not be converted to a value type. [MvxBind] at System.Convert.ToType (System.Object value, System.Type conversionType, IFormatProvider provider, Boolean try_target_to_type) [0x00000] in :0 [MvxBind] at System.Convert.ChangeType (System.Object value, System.Type conversionType, IFormatProvider provider) [0x00000] in :0 [MvxBind] at Cirrious.MvvmCross.Binding.ExtensionMethods.MvxTypeExtensions.MakeSafeValue (System.Type propertyType, System.Object value) [0x00000] in :0 [MvxBind] at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.MakeSafeValue (System.Object value) [0x00000] in :0 [MvxBind]
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue (System.Object value) [0x00000] in :0 [MvxBind]
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (Boolean isAvailable, System.Object value) [0x00000] in :0 MvxBind:Error: 1.34 Problem seen during binding execution for from SearchResult.Count to Visibility - problem InvalidCastException: Null object can not be converted to a value type.MvxBind:Error: 1.34 Problem seen during binding execution for from SearchResult.Count 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 :0 at System.Convert.ChangeType (System.Object value, System.Type conversionType, IFormatProvider provider) [0x00000] in :0

转换器不会被调用。 我原以为在这种情况下会调用转换器,就像在 Silverlight 或 WPF 中一样。

我尝试通过源代码添加 MvvmCross 来自行调试此问题(我从 github 添加了所有必要的项目)。 我在异常调用堆栈中显示的不同方法中设置了断点(示例:Cirrious.MvvmCross.Binding.ExtensionMethods.MvxTypeExtensions.MakeSafeValue),但是在运行时,断点没有命中,它们被调试器忽略了。

奇怪的是,在 MvvmCross 源代码的其他部分(如在 MvxAndroidSetup 中)设置断点有效。

除了转换器的问题,我还想知道如何使断点在 MvvmCross 代码中起作用。如果可能的话,它会让我了解更多,甚至参与开发。

最佳答案

MvvmCross 与 Xaml 的绑定(bind)路径/计算之间存在细微差别。

特别是,我们知道 FallbackValue 的解释和使用方式不同。这种不同的行为:

如果您有其他测试用例要添加到该列表,请将它们上传为 GitHub 存储库并将它们链接到该用例。 (但是不要指望对该绑定(bind)代码进行“非常快速”的更改/修复 - 因为更改它可能会破坏现在依赖它的许多应用程序,并且 MvxValueConverters 通常被构建为更多类型具体比传统的 Microsoft IValueConverter 的)


同时,对于这个特殊情况:

  • FallbackValue 可能对您有用(但可能是枚举回退值,在 xml 中可能很难实现)
  • 或者您可以使用自定义绑定(bind)
  • 或者您可以操纵 ViewModel 结构。

至于断点在 Xamarin.Android 的 PCL 中不起作用,这在很多地方都有记录,并且在超过 6 个月的时间里一直是一个主要的痛苦。根据https://bugzilla.xamarin.com/show_bug.cgi?id=8209这显然现在已修复在 Alpha channel 上,但可能永远不会出现在 4.6 中。

关于android - 当路径上的第一项为空时,与路径的绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17004772/

相关文章:

java - 随机文本应用程序连续显示相同文本 2 次或更多次

android - 如何在 cocos2d-x 中启用多点触控?

android - 通过随机键检索子值-Firebase

用于 Jenkins 管道的 Android 模拟器

c# - Monotouch和XCode 4,风口都去哪了?

silverlight - Silverlight、MonoTouch、MonoDroid 和 Windows Phone 的本地数据库

android - 底部导航布局中谷歌地图 fragment 下的问题定位按钮

c# - 如何从事件中删除/注销事件处理程序?

ios - 向 UIScrollViewDelegate 方法添加代码

xamarin - 使用 Visual Studio 2015 - 社区版 Xamarin 运行什么模拟器>