MVVMCross programmatically changes the start of a ViewModel

A portable class library defines a presentation model. This script usually sounds great, but I considered it. You wrote a universal iOS or Android application that needs to change the start screen / view model. If the application is a telephone, the default presentation model is the login, but if it is a tablet, you want a different presentation model to be used as a start. Is there an override or a way to manage this?

+4
source share
3 answers

See the Wiki section - https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#custom-imvxappstart - this is an example of a program switch:


If more complex startup logic is required, you can use a custom application launch - for example,

public class CustomAppStart
    : MvxNavigatingObject
    , IMvxAppStart
{
    public void Start(object hint = null)
    {
        var auth = Mvx.Resolve<IAuth>();
        if (auth.Check())
        {
            ShowViewModel<HomeViewModel>();
        }
        else
        {
            ShowViewModel<LoginViewModel>();
        }
    }
}

This can then be registered in the application using:

RegisterAppStart(new CustomAppStart());
+4
source

In your App class, you can register an AppStart, which is a splash screen:

RegisterAppStart<SplashScreenViewModel>() 

In this screen saver, you can get a service that checks if it is a tablet or phone. You will need to create a plugin to complete this check. (There are other stackoverflow questions showing how to check this / How to detect a device - Android phone or Android tablet? )

public SplashScreenViewModel(ITabletVerificationService tabletVerificationService)

if(tabletVerificationService.IsTablet())
{
   ShowViewModel<TabletViewModel>
}
else
{
  ShowViewModel<LoginViewModel>
}

, =)

+2

Here is my implementation of this scenario, if it can help:

PCL:

public enum PlateformType
{
    Android,
    iPhone,
    WindowsPhone,
    WindowsStore
}

public interface IPlateformInfos
{
    PlateformType GetPlateformType();
}

public class CustomAppStart
: MvxNavigatingObject
, IMvxAppStart
{
    public void Start(object hint = null)
    {
        var plateformInfos = Mvx.Resolve<IPlateformInfos>();
        var plateformType = plateformInfos.GetPlateformType();
        switch (plateformType)
        {
            default:
                ShowViewModel<MenuViewModel>();
                break;
            case PlateformType.WindowsPhone:
            case PlateformType.WindowsStore:
                ShowViewModel<FirstViewModel>();
                break;
        }
    }
}

PCL App.cs:

RegisterAppStart(new CustomAppStart());

UI (ex: WindowsPhone):

public class PlateformInfos : IPlateformInfos
{
    public PlateformType GetPlateformType()
    {
        return PlateformType.WindowsPhone;
    }
}

UI Setup.cs:

protected override void InitializeFirstChance()
{
    Mvx.RegisterSingleton<IPlateformInfos>(new PlateformInfos());
    base.InitializeFirstChance();
}

Pretty simple way.

0
source

Source: https://habr.com/ru/post/1542955/


All Articles