使用mvvmcross显示视图模型时无法解析当前的顶级活动 [英] Cannot resolve current top activity while showing viewmodel using mvvmcross

查看:103
本文介绍了使用mvvmcross显示视图模型时无法解析当前的顶级活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Mvvmcross应用程序中实现自定义演示者.我想要完成的是:常规导航和片段导航.

I'm implementing a custom presenter in my Mvvmcross application. What I wanted to accomplish is: regular navigation and fragment navigation.

在我的主要活动中,基于此示例,我设法嵌入了几个片段视图: https://github.com /i486dx400/MultiRegionPresenter

In my main activity, I managed to embed several fragment views based on this example: https://github.com/i486dx400/MultiRegionPresenter

在片段运行时,我还想展示不作为片段托管的常规活动.因此,如以下代码片段所示,我扩展了此演示者: https://gist.github.com/JelleDamen/7003702

while the fragments are working, I also wanted to show regular activities, that aren't hosted as a fragment. Therefor I extended this presenter as shown in this snippet: https://gist.github.com/JelleDamen/7003702

问题/错误: 当我显示第二个活动时,它就会显示出来.但是,当我返回到上一个视图(即主机)并再次重新打开同一活动时,它不会显示.输出日志显示:"mvx:警告:无法解决当前的最高活动"

The problem/bug: When I show this second activity, it gets shown. But when I go back to the previous view (which is the host) and re-open the same activity again, it doesn't get shown. The output log says: "mvx:Warning: Cannot Resolve current top activity"

我在做错什么,或者应该怎么做才能告知框架当前最重要的活动是什么?

What am I doing wrong, or what should I do, to inform the framework what activity is the current top activity?

提前谢谢!

推荐答案

出了什么问题?

What is going wrong?

您提供的跟踪行从以下位置显示:

The line of trace you have provided is shown from:

    protected virtual void Show(Intent intent)
    {
        var activity = Activity;
        if (activity == null)
        {
            MvxTrace.Warning("Cannot Resolve current top activity");
            return;
        }
        activity.StartActivity(intent);
    }

https://github.com/MvvmCross/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxAndroidViewPresenter.cs

因此,当调用Show时,似乎没有显示当前的MvvmCross活动.

So it would appear that when Show is called, then there is no current MvvmCross Activity shown.

...,然后查看 https://github .com/i486dx400/MultiRegionPresenter/blob/master/RegionEx.Droid/MainActivity.cs 看起来确实如此-应用程序中的主要活动适用于MvvmCross,但是而是普通的FragmentActivity.

... and looking at https://github.com/i486dx400/MultiRegionPresenter/blob/master/RegionEx.Droid/MainActivity.cs it does appear this is true - the main activity in the app is not adapted for MvvmCross, but is instead just a normal FragmentActivity.

应用程序应如何告知框架当前最重要的活动是什么?

what should an app do to inform the framework what activity is the current top activity?

MvvmCross通常通过截获Activity生命周期事件来跟踪最高活动",特别是Activity创建,启动,重新启动,恢复和销毁的活动.这些显示在 http://developer.android.com/的生命周期图中. reference/android/app/Activity.html

MvvmCross normally tracks the "top activity" by intercepting Activity lifecycle events - specifically the Activity created, started, restarted, resumed and destroyed events. These are shown in the lifecycle diagram in http://developer.android.com/reference/android/app/Activity.html

MvvmCross:

MvvmCross:

  • hooks into these events via the MvxActivityAdapter in https://github.com/MvvmCross/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxActivityAdapter.cs
  • these hooks call the extension methods in https://github.com/MvvmCross/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxActivityViewExtensions.cs
  • these extension methods inform the lifecycle monitor about the lifecycle changes - see https://github.com/MvvmCross/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxAndroidLifeTimeMonitor.cs#L35 -

所有内置的MvvmCross活动类型-MvxActivity,MvxFragmentActivity等-称为自动".可以使用带有最新MVVMCross 的ActionBarSherlock 中所述的步骤将这些适应性扩展到其他活动类型,或者您的应用可以根据需要手动调用其中的一些挂钩.

All the built-in MvvmCross Activity types - MvxActivity, MvxFragmentActivity, etc - call these "automatically". These adaptions can be extended to other Activity types using steps like those described in ActionBarSherlock with latest MVVMCross, or your app can manually call some of these hooks if it prefers.

个人意见:我认为您最好不要遵循 https://github.com/i486dx400/MultiRegionPresenter 太近了. https://github.com中的OnCreate中的代码/i486dx400/MultiRegionPresenter/blob/master/RegionEx.Droid/MainActivity.cs 似乎每次创建MainActivity时都会尝试Start该应用程序-当然,在每个应用程序的生命周期中可能会多次发生应用程序.

Personal opinion: I think you'd be better off not following https://github.com/i486dx400/MultiRegionPresenter too closely. The code in OnCreate in https://github.com/i486dx400/MultiRegionPresenter/blob/master/RegionEx.Droid/MainActivity.cs seems to try to Start the app every time that MainActivity is created - which can, of course, happen multiple times during the lifecycle of each app.

相反,请阅读该示例以及其他示例,例如 http ://motzcod.es/post/60427389481/effective-navigation-in-xamarin-android-part-1

Instead, read that sample and others like http://motzcod.es/post/60427389481/effective-navigation-in-xamarin-android-part-1, https://github.com/jamesmontemagno/Xam.NavDrawer/tree/master/Mvx and http://enginecore.blogspot.ro/2013/06/more-dynamic-android-fragments-with.html - then implement something that suits your navigation needs.

这篇关于使用mvvmcross显示视图模型时无法解析当前的顶级活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆