MVVMCross以编程方式更改启动ViewModel [英] MVVMCross programmatically change start ViewModel
问题描述
可移植类库定义了开始视图模型.这种情况通常听起来不错,但我正在考虑这一点.您已编写了需要更改其初始屏幕/视图模型的iOS通用应用程序或Android.如果应用程序是电话,则默认视图模型是login,但是如果是平板电脑,则需要其他视图模型作为开始.是否有替代方法或控制方法?
The portable class library defines the start view model. This scenario generally sounds great but I was considering this. You have written a iOS universal application or Android that needs to change its start screen / view model. If application is a phone, the default view model is login but if it is tablet, you want a different view model as the start. Is there an override or a way to take control of this?
推荐答案
See the Wiki section - https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#custom-imvxappstart - this has an example of programmatic switching:
如果需要更高级的启动逻辑,则可以使用自定义应用启动-例如
If more advanced startup logic is needed, then a custom app start can be used - e.g.
public class CustomAppStart
: MvxNavigatingObject
, IMvxAppStart
{
public void Start(object hint = null)
{
var auth = Mvx.Resolve<IAuth>();
if (auth.Check())
{
ShowViewModel<HomeViewModel>();
}
else
{
ShowViewModel<LoginViewModel>();
}
}
}
然后可以使用以下方法在App中注册:
This can then be registered in App using:
RegisterAppStart(new CustomAppStart());
这篇关于MVVMCross以编程方式更改启动ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!