在创建时无法解析IMvxMainThreadDispatcher类型的参数调度程序的参数

时间:2017-03-01 10:11:53

标签: c# android xamarin mvvmcross

我的Android应用程序在生产环境中崩溃,我们的崩溃报告工具是XamarinInsights,它转储了以下堆栈

at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor) [0x00066] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x0002a] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.Mvx.IocConstruct[T] () [0x00005] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.Mvx+<>c__13`2[TInterface,TType].<LazyConstructAndRegisterSingleton>b__13_0 () [0x00000] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.IoC.MvxSimpleIoCContainer+<>c__DisplayClass33_0`1[TInterface].<RegisterSingleton>b__0 () [0x00000] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.IoC.MvxSimpleIoCContainer+ConstructingSingletonResolver.Resolve () [0x00028] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, MvvmCross.Platform.IoC.MvxSimpleIoCContainer+IResolver resolver, System.Object& resolved) [0x00041] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, System.Nullable`1[T] requiredResolverType, System.Object& resolved) [0x0002e] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, System.Object& resolved) [0x00000] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.Resolve (System.Type t) [0x00011] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.Resolve[T] () [0x00000] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MvvmCross.Platform.Mvx.Resolve[TService] () [0x00005] in <4ddde23419c5494288c799fcdbb0f189>:0 
at MyNamespace.Android.PushNotificationListener.SendNotification (System.String message, MyNamespace.Core.NotificationPayload payload) [0x00137] in <03e2d64cacc54ebebbfb6133dd9c33ae>:0 

我认为当我尝试解析和构建MainThreadDispatcher时会抛出异常。但我无法理解为什么。

    private IMvxMainThreadDispatcher _dispatcher = Mvx.Resolve<IMvxMainThreadDispatcher>();

    private void ShowToastMessage(string message, double duration)
    {
            _dispatcher.RequestMainThreadAction(() =>
            { 
                ...
            });
    }

2 个答案:

答案 0 :(得分:2)

每次收到推送时尝试初始化MvvmCross系统,因为执行可以在不同的上下文中完成(卸载应用程序时),但推送通知服务处于活动状态。

MvvmCross initialization

答案 1 :(得分:2)

在重新启动进程后使用IntentServiceBroadcastReceiver时,这是一个非常常见的问题。 MvvmCross IoC容器通常由启动屏幕设置。在这些情况下不显示闪屏,因此Mvx IoC基本上未初始化。您可以通过确保在收到推送通知后初始化MvvmCross设置来解决问题。

var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(this.ApplicationContext);
setupSingleton.EnsureInitialized();

MvvmCross有MvxIntentServiceMvxBroadcastReceiver。如果您使用这些对象,则会自动为您初始化MvvmCross设置。