【问题标题】:Cannot resolve current top activity while showing viewmodel using mvvmcross使用 mvvmcross 显示视图模型时无法解析当前的顶级活动
【发布时间】:2013-10-24 04:55:51
【问题描述】:

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

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

在片段工作的同时,我还想展示不作为片段托管的常规活动。因此我扩展了这个演示者,如下所示:https://gist.github.com/JelleDamen/7003702

问题/错误: 当我展示第二个活动时,它会显示出来。但是当我返回上一个视图(即主机)并再次重新打开相同的活动时,它不会显示出来。输出日志显示:“mvx:Warning: Cannot Resolve current top activity”

我做错了什么,或者我应该怎么做,以通知框架什么活动是当前的顶级活动?

提前致谢!

【问题讨论】:

    标签: c# android android-fragments mvvmcross presenter


    【解决方案1】:

    出了什么问题?

    您提供的跟踪线显示来自:

        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 Activity。

    ...看看https://github.com/i486dx400/MultiRegionPresenter/blob/master/RegionEx.Droid/MainActivity.cs,看来这是真的 - 应用程序中的主要活动适用于 MvvmCross,而只是一个普通的 FragmentActivity。

    应用应如何通知框架当前最热门的活动是什么活动?

    MvvmCross 通常通过拦截 Activity 生命周期事件来跟踪“顶级活动”——特别是 Activity 创建、启动、重新启动、恢复和销毁事件。这些在http://developer.android.com/reference/android/app/Activity.html的生命周期图中显示

    MvvmCross:

    所有内置的 MvvmCross Activity 类型 - MvxActivity、MvxFragmentActivity 等 - “自动”调用这些。可以使用ActionBarSherlock with latest MVVMCross 中描述的步骤将这些适配扩展到其他 Activity 类型,或者您的应用可以根据需要手动调用其中的一些钩子。


    个人意见:我认为你最好不要太关注https://github.com/i486dx400/MultiRegionPresenterhttps://github.com/i486dx400/MultiRegionPresenter/blob/master/RegionEx.Droid/MainActivity.cs 中的 OnCreate 中的代码似乎会在每次创建 MainActivity 时尝试 Start 应用程序 - 当然,这可以在每个应用程序的生命周期中多次发生。

    相反,请阅读该示例和其他示例,例如 http://motzcod.es/post/60427389481/effective-navigation-in-xamarin-android-part-1https://github.com/jamesmontemagno/Xam.NavDrawer/tree/master/Mvxhttp://enginecore.blogspot.ro/2013/06/more-dynamic-android-fragments-with.html - 然后实现适合您导航需求的内容。

    【讨论】:

    • 谢谢斯图尔特!我的主要活动确实是一个常规的片段活动。每次我轮换这个活动时,应用程序都会启动,这是不可取的。我会查看您建议的链接,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多