c# - OnPropertyChanged 在 xamarin.forms 应用程序中的 android 上引发异常

标签 c# android xamarin inotifypropertychanged xamarin.forms

我正在使用 Xamarin.Forms 制作一个 Android 应用程序。我的 iOS 应用程序已经完成并发布了。 我的 Android 版本不会停止抛出多个异常。

第一个异常是NullReferenceException。这会破坏以下代码 fragment :

handler(this, new PropertyChangedEventArgs(propertyName));

这是在我用于模型的自定义 NotifyBase 类中的 OnPropertyChanged 方法内。 (如果我从方法中删除除此行之外的所有内容,也会失败。)

调试输出只是告诉我这一点:

System.NullReferenceException: Object reference not set to an instance of an object
An unhandled exception occured.
Exception:

后面跟着一个 TargetInvocableException 给出以下堆栈跟踪:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
07-16 17:37:47.916 I/MonoDroid(27212): UNHANDLED EXCEPTION:        System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object
07-16 17:37:47.916 I/MonoDroid(27212):07-16 17:37:47.916 I/MonoDroid(27212):   at Xamarin.Forms.Forms+AndroidPlatformServices.BeginInvokeOnMainThread (System.Action action) [0x00000] in <filename unknown>:0 
07-16 17:37:47.916 I/MonoDroid(27212):   at Xamarin.Forms.Device.BeginInvokeOnMainThread (System.Action action) [0x00000] in <filename unknown>:0 
07-16 17:37:47.916 I/MonoDroid(27212):   at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x00000] in <filename unknown>:0 
07-16 17:37:47.916 I/MonoDroid(27212):   at (wrapper delegate-invoke) <Module>:invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
07-16 17:37:47.916 I/MonoDroid(27212):   at (wrapper delegate-invoke) <Module>:invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
07-16 17:37:47.916 I/MonoDroid(27212):   at TransMission.Mobile.Helper.NotifyBase.OnPropertyChanged (System.String propertyName) [0x00087] in c:\Users\Frederik.TECNO\Documents\Visual Studio 2012\Projects\TransMission\TransMission.Mobile\TransMission.Mobile\Helper\NotifyBase.cs:68 
07-16 17:37:47.916 I/MonoDroid(27212):   at TransMission.Mobile.Helper.NotifyBase.SetField[DateTime] (System.DateTime& field, DateTime value, System.String propertyName) [0x000a2] in c:\Users\Frederik.TECNO\Documents\Visual Studio 2012\Projects\TransMission\TransMission.Mobile\TransMission.Mobile\Helper\NotifyBase.cs:102 
07-16 17:37:47.916 I/MonoDroid(27212):   at TransMission.Mobile.Core.TransMissionCore.set_Date (DateTime value) [0x00001] in c:\Users\Frederik.TECNO\Documents\Visual Studio 2012\Projects\TransMission\TransMission.Mobile\TransMission.Mobile\Core\TransMissionCore.cs:275 
07-16 17:37:47.916 I/MonoDroid(27212):   at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
07-16 17:37:47.916 I/MonoDroid(27212):   at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
07-16 17:37:47.916 I/MonoDroid(27212):   --- End of inner exception stack trace ---
07-16 17:37:47.916 I/MonoDroid(27212): at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <IL 0x00062, 0x0033c>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.BindingExpression.ApplyCore (object,Xamarin.Forms.BindableObject,Xamarin.Forms.BindableProperty,bool) <IL07-16 17:37:47.916 I/MonoDroid(27212): at System.Reflection.MethodBase.Invoke (object,object[]) <IL 0x00006, 0x0008f>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.BindingExpression.ApplyCore (object,Xamarin.Forms.BindableObject,Xamarin.Forms.BindableProperty,bool) <IL 0x00295, 0x0162f>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.BindingExpression.Apply (bool) <IL 0x00041, 0x0018b>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.Binding.Apply (bool) <IL 0x00027, 0x00113>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.BindableObject/<>c__DisplayClass1.<SetValueCore>b__0 () <IL 0x00100, 0x00493>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,bool,bool,bool) <IL 0x001b4, 0x009d3>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.BindingExpression.ApplyCore (object,Xamarin.Forms.BindableObject,Xamarin.Forms.BindableProperty,bool) <IL 0x001f1, 0x011db>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.BindingExpression.Apply (object,Xamarin.Forms.BindableObject,Xamarin.Forms.BindableProperty) <IL 0x0007e, 0x003bf>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.Binding.Apply (object,Xamarin.Forms.BindableObject,Xamarin.Forms.BindableProperty) <IL 0x0004d, 0x00237>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.BindableObject.ApplyBindings (object) <IL 0x0003a, 0x002af>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject,object) <IL 0x00056, 0x0029b>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.Element.OnBindingContextChanged () <IL 0x0001c, 0x00157>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.View.OnBindingContextChanged () <IL 0x0003b, 0x002a3>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject,object) <IL 0x0005c, 0x002bf>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.Element.set_Parent (Xamarin.Forms.Element) <IL 0x00020, 0x00107>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.Element.OnChildAdded (Xamarin.Forms.Element) <IL 0x00002, 0x00053>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.Page.OnInternalAdded (Xamarin.Forms.VisualElement) <IL 0x00015, 0x000bf>
07-16 17:37:47.916 I/MonoDroid(27212): at Xamarin.Forms.Page.InternalChildrenOnCollectionChanged (object,System.Collections.Specialized.NotifyCollectionC

未处理的异常:

之后又是一个 TargetInitationException 给出了这个堆栈跟踪:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
07-16 17:41:19.182 E/mono    (27212): 
07-16 17:41:19.182 E/mono    (27212): Unhandled Exception:
07-16 17:41:19.182 E/mono    (27212):   at (wrapper delegate-invoke) <Module>:invoke_void__this___object_PropertyChangedEventArgs (object,System.Compon07-16 17:41:19.182 E/mono    (27212): System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object
07-16 17:41:19.182 E/mono    (27212):   at Xamarin.Forms.Forms+AndroidPlatformServices.BeginInvokeOnMainThread (System.Action action) [0x00000] in <filename unknown>:0 
07-16 17:41:19.182 E/mono    (27212):   at Xamarin.Forms.Device.BeginInvokeOnMainThread (System.Action action) [0x00000] in <filename unknown>:0 
07-16 17:41:19.182 E/mono    (27212):   at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x00000] in <filename unknown>:0 
07-16 17:41:19.182 E/mono    (27212):   at (wrapper delegate-invoke) <Module>:invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
07-16 17:41:19.182 E/mono    (27212):   at (wrapper delegate-invoke) <Module>:invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
07-16 17:41:19.182 E/mono    (27212):   at TransMission.Mobile.Helper.NotifyBase.OnPropertyChanged (System.String propertyName
07-16 17:41:19.182 E/mono-rt (27212): [ERROR] FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object
07-16 17:41:19.182 E/mono-rt (27212):   at Xamarin.Forms.Forms+AndroidPlatformServices.BeginInvokeOnMainThread (System.Action action) [0x00000] in <filename unknown>:0 
07-16 17:41:19.182 E/mono-rt (27212):   at Xamarin.Forms.Device.BeginInvokeOnMainThread (System.Action action) [0x00000] in <filename unknown>:0 
07-16 17:41:19.182 E/mono-rt (27212):   at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x00000] in <filename unknown>:0 
07-16 17:41:19.182 E/mono-rt (27212):   at (wrapper delegate-invoke) <Module>:invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
07-16 17:41:19.182 E/mono-rt (27212):   at (wrapper delegate-invoke) <Module>:invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
07-16 17:41:19.182 E/mono-rt (27212):   at TransMission.Mobile.Helper.NotifyBase.OnPropertyChanged (System.String
Das Programm "Mono" wurde mit Code 0 (0x0) beendet.

正如我的调试输出所述,这似乎发生在 DateTime 属性上,该属性设置为 DatePicker 的最小值:

07-16 17:32:45.761 I/mono-stdout(27212): DEBUG     - Changing value from '01.01.0001 00:00:00' to '01.01.1900 00:00:00'.
07-16 17:32:45.771 I/mono-stdout(27212): DEBUG     - Raising PropertyChanged for: 'TransMissionCore.Date'.

我的代码在 iOS 上完美运行。但在 Android 上,这就是一个阻碍。 我使用最新版本的 Xamarin.Forms (1.2.1.6229) 和 1.1.1.6206 尝试了我的应用程序,但这没有什么区别。

有某种解决方法吗?或者我犯了一个错误? 请大家给点建议。

最佳答案

此问题已在最新版本的 xamarin 表单中修复。 我没有对最新版本中异常的堆栈跟踪给予足够的关注。 我认为堆栈跟踪是相同的,但它们在某些部分有所不同 这似乎有另一个来源,来 self 的应用程序的另一部分。

关于c# - OnPropertyChanged 在 xamarin.forms 应用程序中的 android 上引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24785263/

相关文章:

c# - 全局设置 String.Compare/CompareInfo.Compare 为 Ordinal

android - Google admob “Problem linking app. Please try again later.”

c# - 自定义控件中的选择器

c# - Mono 中的 XML 序列化问题

c# - 如何检查 MongoDB C# 中的索引(是否存在),因为 IndexExist 从新版本中已弃用

没有嵌套在父类中的 C# 私有(private)类

android - RxJava 运算符结合两个可观察对象并在第一个可观察对象发出时立即发出

带有平台签名的 Android Gradle 构建

ios - 将带有列表的 map 添加到移动应用程序的表单?

iOS 模拟器没有启动