MvvmCross:如何在Android上从常规视图导航到Mvvm视图模型? [英] MvvmCross: How to navigate from regular view to Mvvm viewmodel on Android?
问题描述
我正在将我的应用程序缓慢迁移到Android上的MvvmCross.我还没有将所有Activity都转换为ViewModels.因此,我需要从Mvvm控制的活动导航到常规活动.为此,我创建了自己的Presenter并拦截了Show方法,并执行了自己的StartActivity.那似乎行得通.但是,现在我需要朝另一个方向发展,让我的常规活动返回到原来的Mvvm控制的活动.我尝试仅使用CLEAR_TOP标志在ViewModel上执行StartActivity,但出现错误:
I am slowly migrating my app over to MvvmCross on Android. I have not converted all Activities over to ViewModels yet. Therefore, I need to navigate from an Mvvm-controlled Activity to a regular Activity. To do this, I made my own Presenter and intercepted the Show method and did my own StartActivity. That seemed to work. However, now I need to go the other direction and have my regular Activity go back to the original Mvvm-controlled Activity. I tried just doing a StartActivity on the ViewModel using CLEAR_TOP flag, but I got an error:
"Null Extras seen on Intent when creating ViewModel - this should not happen - have you tried to navigate to an MvvmCross View directly?"
如何从常规活动"返回到Mvvm控制的原始活动?
How can I go back to the original Mvvm-controlled activity from a regular Activity?
推荐答案
简单...
要从任何标准的Android Activity
返回,您可以简单地要求Activity
使用Finish()
To go back from any standard Android Activity
, you can simply ask the Activity
to close using Finish()
但除此之外...
如果相反,您想前进到MvvmCross View
,那么您需要对MvvmCross内部有一点了解:特别是关于MvvmCross导航在概念上如何在ViewModels
之间而不是在Activities
,Pages
之间发生或UIViewControllers
.
If instead you want to go forwards to an MvvmCross View
, then you need to know a little about MvvmCross internals: in particular about how MvvmCross navigation conceptually happens between ViewModels
rather than between Activities
, Pages
or UIViewControllers
.
如果要前进到新的ViewModel
,则可以使用IMvxViewDispatcher
单例进行操作-
If you want to go forwards to a new ViewModel
, then you can do this using the IMvxViewDispatcher
singleton - how to do this is shown in Show view from non-view/viewmodel in mvvmcross
如果随后您想从当前的ViewModel
返回,则可以尝试在ViewModel
中调用Close(this)
-默认情况下,在Android上,它将映射到Finish()
,在触摸时会映射到GoBack()
If you then later want to go back from the current ViewModel
, then you can try calling Close(this)
within the ViewModel
- by default, on Android this will map to Finish()
, on Touch to PopViewController
, on WpDev to GoBack()
这篇关于MvvmCross:如何在Android上从常规视图导航到Mvvm视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!