我有以下代码:
public void LoadComments ()
{
IsLoading = true;
Console.WriteLine (Recomendation.Recomendation.ID);
List<Comment> tmpcomments = CommunicationManager.Comments.GetComments (Core.AppConfiguration.Instance.User.ID, Page, Recomendation.Recomendation.ID);
Dispatcher.RequestMainThreadAction (() =>
{
try
{
Comments.Add (Recomendation);
foreach (var item in tmpcomments)
{
var vm = new CommentViewModel (item);
Comments.Add (vm);
}
IsLoading = false;
}
catch (Exception ex)
{
}
});
}
我偶尔会从这一行获得以下异常:Comments.Add (Recomendation);
吞下异常并导致应用程序崩溃。这是堆栈跟踪:
[mono-rt] Stacktrace:
[mono-rt]
[mono-rt] at <unknown> <0xffffffff>
[mono-rt] at (wrapper managed-to-native) object.wrapper_native_0x4083eba1 (intptr,intptr,intptr,intptr) <IL 0x00026, 0xffffffff>
[mono-rt] at (wrapper delegate-invoke) <Module>.invoke_void__this___intptr_intptr_intptr_intptr (intptr,intptr,intptr,intptr) <IL 0x00062, 0xffffffff>
[mono-rt] at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr) [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:600
[mono-rt] at Android.Widget.BaseAdapter.NotifyDataSetChanged () [0x00058] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.Widget.BaseAdapter.cs:305
[mono-rt] at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapter.RealNotifyDataSetChanged () <IL 0x00003, 0x00087>
[mono-rt] at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapter.NotifyDataSetChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs) <IL 0x00002, 0x00063>
[mono-rt] at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapter.OnItemsSourceCollectionChanged (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs) <IL 0x00003, 0x0006b>
[mono-rt] at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___object_object (object,intptr,intptr,intptr) <IL 0x0005a, 0xffffffff>
[mono-rt] at <unknown> <0xffffffff>
[mono-rt] at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) <IL 0x00030, 0xffffffff>
[mono-rt] at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <IL 0x0004a, 0x0027f>
[mono-rt] at System.Reflection.MethodBase.Invoke (object,object[]) <IL 0x00006, 0x0008f>
[mono-rt] at Cirrious.CrossCore.WeakSubscription.MvxWeakEventSubscription`2.OnSourceEvent (object,TEventArgs) <IL 0x00032, 0x00173>
[mono-rt] at (wrapper delegate-invoke) <Module>.invoke_void__this___object_NotifyCollectionChangedEventArgs (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs) <IL 0x00059, 0xffffffff>
[mono-rt] at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs) <IL 0x00017, 0x000f7>
[mono-rt] at System.Collections.ObjectModel.ObservableCollection`1.InsertItem (int,T) <IL 0x0001c, 0x000d3>
[mono-rt] at System.Collections.ObjectModel.Collection`1.Add (T) <IL 0x0000f, 0x000c3>
[mono-rt] at TasteITAndroidClient.RecomendationPageViewModel/<>c__DisplayClass7.<LoadComments>b__6 () [0x00002] in e:\Software Projects\TasteIT\TasteIT\TasteITAndroidClient\Shared\ViewModels\Pages\RecomendationPageViewModel.cs:86
[mono-rt] at Cirrious.CrossCore.Core.MvxMainThreadDispatcher.ExceptionMaskedAction (System.Action) <IL 0x00003, 0x00093>
[mono-rt] at Cirrious.MvvmCross.Droid.Views.MvxAndroidMainThreadDispatcher/<>c__DisplayClass1.<RequestMainThreadAction>b__0 (object) <IL 0x00006, 0x0004b>
[mono-rt] at Android.App.SyncContext/<Post>c__AnonStorey2E.<>m__1F () [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:23
[mono-rt] at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:32
[mono-rt] at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Java.Lang.IRunnable.cs:71
[mono-rt] at (wrapper dynamic-method) object.3cc3d616-7089-41d9-a968-d58c100cf59e (intptr,intptr) <IL 0x00011, 0x0003b>
[mono-rt] at (wrapper native-to-managed) object.3cc3d616-7089-41d9-a968-d58c100cf59e (intptr,intptr) <IL 0x00022, 0xffffffff>
[mono-rt]
[mono-rt] =================================================================
[mono-rt] Got a SIGSEGV while executing native code. This usually indicates
[mono-rt] a fatal error in the mono runtime or one of the native libraries
[mono-rt] used by your application.
[mono-rt] =================================================================
[mono-rt]
如上所述,这是一个使用以下适配器的多态MvxLiseView:
public class PostPageListAdapter :MvxAdapter
{
public PostPageListAdapter(Context context, IMvxAndroidBindingContext bindingContext)
: base(context, bindingContext)
{
}
public override int GetItemViewType(int position)
{
var item = GetRawItem(position);
if (item is RecomendationViewModel)
return 0;
return 1;
}
public override int ViewTypeCount
{
get { return 2; }
}
protected override View GetBindableView(View convertView, object source, int templateId)
{
try
{
if (source is RecomendationViewModel)
{
templateId = Resource.Layout.ListHeader_RecomendationPage;
return base.GetBindableView (convertView, source, templateId);
}
else
if (source is CommentViewModel)
{
templateId = Resource.Layout.Template_Comment;
}
View v = base.GetBindableView (convertView, source, templateId);//
return v;
}
catch (Exception ex)
{
return new View (convertView.Context);
}
}
}
它没有在任何地方缓存,只是崩溃我的应用程序。它变得非常令人沮丧...