1

在 mvvmCross 中使用片段时出现以下问题。我从https://github.com/slodge/MvvmCross-Tutorials/tree/master/Fragments借用了代码。https://github.com/slodge/MvvmCross-Tutorials/blob/master/Fragments/FragmentSample.UI.Droid/Setup.cs中的 Framepresenter 类解析了在 show 方法中显示视图模型的请求

公共类 CustomPresenter
        : MvxAndroidViewPresenter
        , ICustomPresenter
    {
        私人词典_dictionary = new Dictionary();

        公共覆盖无效显示(MvxViewModelRequest 请求)
        {
            IFragmentHost 主机;
            if (_dictionary.TryGetValue(request.ViewModelType, out host))
            {
                if (host.Show(request))
                {
                    返回;
                }
            }

            base.Show(请求);
        }
...

在第一次运行中,一切正常。如果我使用 android 的“返回”按钮关闭应用程序并再次打开它,我将在基类 (MvxAndroidViewPresenter) 的 Show 方法中得到一个空指针异常。这是因为顶级活动不再存在。MvxAndroidViewPresenter 中的属性 Activity 返回 null。重新启动应用程序时如何重新创建顶级活动?

我得到以下异常

06-26 16:06:28.102 I/MonoDroid(6791):未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例
06-26 16:06:28.102 I/MonoDroid(6791):在 Cirrious.MvvmCross.Droid.Views.MvxAndroidViewPresenter.Show (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest) [0x0000f] 在 d:\Projekte\MvvmCross\mvvmcross-20130621 \Cirrious\Cirrious.MvvmCross.Droid\Views\MvxAndroidViewPresenter.cs:29
06-26 16:06:28.102 I/MonoDroid(6791):在 ManCockpit.UI.Droid.CockpitFragmentPresenter.Show (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest) [0x00040] 在 d:\Projekte\ManCockpitV2\ManCockpit.UI.Droid \CockpitFragmentPresenter.cs:41
06-26 16:06:28.102 I/MonoDroid(6791):在 Cirrious.MvvmCross.Droid.Views.MvxAndroidViewDispatcher/c__DisplayClass1.b__0 () [0x00000] in d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious\Cirrious。 MvvmCross.Droid\Views\MvxAndroidViewDispatcher.cs:27
06-26 16:06:28.102 I/MonoDroid(6791):在 Cirrious.MvvmCross.Droid.Views.MvxAndroidViewDispatcher.ShowViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest) [0x00015] 在 d:\Projekte\MvvmCross\mvvmcross-20130621 \Cirrious\Cirrious.MvvmCross.Droid\Views\MvxAndroidViewDispatcher.cs:27
06-26 16:06:28.102 I/MonoDroid(6791):在 Cirrious.MvvmCross.ViewModels.MvxNavigatingObject.ShowViewModelImpl(System.Type,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross。 ViewModels.MvxRequestedBy)
06-26 16:06:28.102 I/MonoDroid(6791):在(包装动态方法)objec06-26 16:06:28.102 I/MonoDroid(6791):在 Cirrious.MvvmCross.ViewModels.MvxNavigatingObject.ShowViewModel(系统。类型,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.MvxRequestedBy)
06-26 16:06:28.102 I/MonoDroid (6791): 在 Cirrious.MvvmCross.ViewModels.MvxNavigatingObject.ShowViewModel (Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.MvxBundle,Cirrious.MvvmCross.ViewModels.MvxRequestedBy)
06-26 16:06:28.102 I/MonoDroid(6791):在 Cirrious.MvvmCross.ViewModels.MvxAppStart`1.Start(对象)
06-26 16:06:28.102 I/MonoDroid (6791): 在 Cirrious.MvvmCross.Droid.Views.MvxSplashScreenActivity.TriggerFirstNavigate () [0x00007] 在 d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious\Cirrious.MvvmCross。 Droid\Views\MvxSplashScreenActivity.cs:82
06-26 16:06:28.102 I/MonoDroid(6791):在 Cirrious.MvvmCross.Droid.Views.MvxSplashScreenActivity.InitializationComplete () [0x00001] 在 d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious\Cirrious.MvvmCross。 Droid\Views\MvxSplashScreenActivity.cs:76
06-26 16:06:28.102 I/MonoDroid(6791):在 Cirrious.MvvmCross.Droid.Platform.MvxAndroidSetupSingleton.InitialiseFromSplashScreen(Cirrious.MvvmCross.Droid.Views.IMvxAndroidSplashScreenActivity)[0x00036] 在 d:\Projekte\MvvmCross -20130621\Cirrious\Cirrious.MvvmCross.Droid\Platform\MvxAndroidSetupSingleton.cs:76
06-26 16:06:28.102 I/MonoDroid(6791):在 Cirrious.MvvmCross.Droid.Views.MvxSplashScreenActivity.OnCreate (Android.OS.Bundle) [0x00014] 在 d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious \Cirrious.MvvmCross.Droid\Views\MvxSplashScreenActivity.cs:47
06-26 16:06:28.102 I/MonoDroid(6791):在 ManCockpit.UI.Droid.SplashScreenActivity.OnCreate (Android.OS.Bundle) [0x0002e] 在 d:\Projekte\ManCockpitV2\ManCockpit.UI.Droid\SplashScreenActivity .cs:105
06-26 16:06:28.102 I/MonoDroid(6791):在 /Users/builder/data/lanes/monodroid-lion-bs1/0cc7ae3b 中的 Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00010] /source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:1859
06-26 16:06:28.102 I/MonoDroid(6791):在(包装动态方法)对象.08473369-0d89-4344-a57c-40e97793bab8(intptr,intptr,intptr)
4

1 回答 1

1

这看起来与 Fragments 无关 - 而是一个已知错误 - https://github.com/slodge/MvvmCross/issues/316

对此的修复尚未发布到 nuget。

要解决此问题,您可以自己构建修复程序。或者,您可以通过添加以下内容来覆盖启动画面类中的行为:

     private bool _isResumed;

     protected override void OnResume()
     {
         _isResumed = true;
         base.OnResume();
     }

     protected override void OnPause()
     {          
        _isResumed = false;
        base.OnPause();
     }

     public override void InitializationComplete()
     {
        if (!_isResumed)
            return;

         TriggerFirstNavigate();
     } 
于 2013-06-26T14:15:20.647 回答