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?
问题:
回答1:
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>();
}
}
}
This can then be registered in App using:
RegisterAppStart(new CustomAppStart());
回答2:
In your App class you could register an AppStart that is a splash screen:
RegisterAppStart<SplashScreenViewModel>()
In that splash screen you could receive a service that verifies if it's a tablet or a phone. You would need to create a plugin to make this verification. (There are other stackoverflow questions showing how to verify this / How to detect device is Android phone or Android tablet? )
public SplashScreenViewModel(ITabletVerificationService tabletVerificationService)
Then you would simply change screen according to this service
if(tabletVerificationService.IsTablet())
{
ShowViewModel<TabletViewModel>
}
else
{
ShowViewModel<LoginViewModel>
}
Hope it helps =)
回答3:
Here's my implementation of this scenario, if it could 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.