我正在使用MvvmCross开发Xamarin.Android应用程序。 每次我在RELEASE配置下启动应用程序时,它都会在启动时崩溃并出现以下错误:
Cirrious.CrossCore.Exceptions.MvxException:无法构造和 为类型DroidApp.Core.ViewModels.MainViewModel初始化ViewModel 来自定位器MvxDefaultViewModelLocator - 查看MvxTrace了解更多信息 信息
此应用程序在DEBUG配置下工作正常。 在我更新我的代码以使用MvvmCross 3.1
后,问题开始出现堆栈跟踪的完整输出如下:
03-16 03:53:04.110 I / MonoDroid(593):UNHANDLED EXCEPTION: Cirrious.CrossCore.Exceptions.MvxException:无法构造和 从类型Pcl.Core.ViewModels.MainViewModel初始化ViewModel 定位器MvxDefaultViewModelLocator - 查看MvxTrace了解更多信息 信息03-16 03:53:04.110 I / MonoDroid(593):at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.IMvxViewModelLocator) 03-16 03:53:04.110 I / MonoDroid(593):at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest,Cirrious.MvvmCross.ViewModels.IMvxBundle) 03-16 03:53:04.110 I / MonoDroid(593):at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.ViewModelFromRequest (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest,Cirrious.MvvmCross.ViewModels.IMvxBundle) 03-16 03:53:04.110 I / MonoDroid(593):at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.CreateViewModelFromIntent (Android.Content.Intent,Cirrious.MvvmCross.ViewModels.IMvxBundle)03-16 03:53:04.110 I / MonoDroid(593):at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.Load (Android.Content.Intent,Cirrious.MvvmCross.ViewModels.IMvxBundle,System.Type的) 03-16 03:53:04.110 I / MonoDroid(593):at Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions.LoadViewModel (Cirrious.MvvmCross.Droid.Views.IMvxAndroidView,Cirrious.MvvmCross.ViewModels.IMvxBundle) 03-16 03:53:04.110 I / MonoDroid(593):at Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions /<> C_ DisplayClass3.b _1 ()03-16 03:53:04.110 I / MonoDroid(593):at Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate (Cirrious.MvvmCross.Views.IMvxView,System.Func
1<Cirrious.MvvmCross.ViewModels.IMvxViewModel>) <IL 0x00013, 0x00113> 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions.OnViewCreate (Cirrious.MvvmCross.Droid.Views.IMvxAndroidView,Android.OS.Bundle) <IL 0x00073, 0x0031f> 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.MvvmCross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled (object,Cirrious.CrossCore.Core.MvxValueEventArgs
1) 03-16 03:53:04.110 I / MonoDroid(593):at (包装器委托调用) System.EventHandler1<Cirrious.CrossCore.Core.MvxValueEventArgs
1&GT; .invoke_void_ 此 __ object_TEventArgs (对象,Cirrious.CrossCore.Core.MvxValueEventArgs1<Android.OS.Bundle>) <0x00067> 03-16 03:53:04.110 I/MonoDroid( 593): at Cirrious.CrossCore.Core.MvxDelegateExtensionMethods.Raise<Android.OS.Bundle> (System.EventHandler
1&GT;,对象,Android.OS.Bundle) &LT; 0x000c3&GT; 03-16 03:53:04.110 I / MonoDroid(593):at Cirrious.CrossCore.Droid.Views.MvxEventSourceActivity.OnCreate (Android.OS.Bundle)03-16 03:53:04.110 I / MonoDroid(593):在MyApp.Droid.Common.MvxActivityBase.OnCreate (Android.OS.Bundle)03-16 03:53:04.110 I / MonoDroid(593):在MyApp.Droid.Views.MainView.OnCreate (Android.OS.Bundle)03-16 03:53:04.110 I / MonoDroid(593):at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr)03-16 03:53:04.110 I / MonoDroid(593):at(包装器动态方法) object.e62191de-ee25-45fe-b902-796a741820a1(intptr,intptr,intptr)未处理的异常:Cirrious.CrossCore.Exceptions.MvxException:无法构造和 从类型Pcl.Core.ViewModels.MainViewModel初始化ViewModel 定位器MvxDefaultViewModelLocator - 查看MvxTrace了解更多信息 03-16 03:53:07.080 E / mono-rt(593):[ERROR]致命 UNHANDLED EXCEPTION:Cirrious.CrossCore.Exceptions.MvxException: 无法为类型构造和初始化ViewModel 来自定位器的Pcl.Core.ViewModels.MainViewModel MvxDefaultViewModelLocator - 有关更多信息,请查看MvxTrace 03-16 03:53:07.080 E / mono-rt(593):at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest请求,IMvxBundle savedState,IMvxViewModelLocator viewModelLocator)[0x00000] in :0 03-16 03:53:07.080 E / mono-rt(593):at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest请求,IMvxBundle savedState)[0x00000] in:0 03-16 03:53:07.080 E / mono-rt(593):at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.ViewModelFromRequest (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest viewModelRequest, IMvxBundle savedState)[0x00000] in:0 03-16 03:53:07.080 E / mono-rt(593):at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.CreateViewModelFromIntent (Android.Content.Intent intent,IMvxBundle savedState)[0x00000] 程序&#39; Mono&#39;已退出代码0(0x0)。
答案 0 :(得分:6)
根据提供的信息,我不知道是什么导致了这个问题。
上投放仅当MvxDefaultViewModelLocator
返回false
时才会出现此情况。
当MvxDefaultViewModelLocator
返回false
时,它始终会记录一条消息,说明原因 - 请参阅
https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross/ViewModels/MvxDefaultViewModelLocator.cs#L31
要调试它我会:
base.OnCreate()
中的MainView
来电添加异常处理程序,看看是否能解释问题。MainViewModel
构造函数添加一些跟踪和/或断点 - 是否会调用它?答案 1 :(得分:1)
我遇到了同样的问题,但斯图尔特的小费有助于弄明白。
我有ContactsViewModel,它通过IoC引擎使用ContactsService。我的MainViewModel包含ContactsViewModel(因为我使用标签栏,就像在教程中一样)。
我只是试图删除ContactsViewModel中的服务调用,一切都很好。我在ctor中找到了错误,它说“操作无法完成。(ABAddressBookErrorDomain错误1.)”,所以这是我的服务错误。
因此,如果有人遇到此错误,请记住,当ctor(特别是)发生任何异常时,始终会抛出令人困惑的“TargetInvocationException”。