0
votes

I'm new to xamarin and the MvvmCross framework. I'm currently creating a multi platform application for android, windows phone and iOS. Im currently having a problem finding out what platform the app is running on.

What i want to do is to hava an if statement in the app.cs file. I want to check if this is an iOS application then do stuff, else do this. But I haven't found any good way to do this, and im not even sure it can be done in this file

Here is my code so far:

using Cirrious.CrossCore;
using Cirrious.CrossCore.IoC;
using Cirrious.MvvmCross.ViewModels;
using tax.Mobile.Core.Interfaces;
using tax.Mobile.Core.Logic;strong text
namespace tax.Mobile.Core
{
public partial class App : MvxApplication
{
    public override void Initialize()
    {
        CreatableTypes()
            .EndingWith("Service")
            .AsInterfaces()
            .RegisterAsLazySingleton();

#if (__iOS__) 
        RegisterAppStart<ViewModels.FirstViewModel>();
#else
            RegisterAppStart<ViewModels.SearchViewModel>();
#endif
        Mvx.RegisterType<IWebService, MockWebService>();     
    }

}
}

Thanks!

1
Are the parentheses part of the symbol defined in the project properties? Normally you wouldn't put them. - valdetero
Are you getting any errors? What's not working with the code you provided? - valdetero
Sounds like a job for IoC/DI, not for if/else flow control. - NovaJoe
Wait, so you want the app to start with different views depending on the platform? - NovaJoe

1 Answers

0
votes

This will not work, PCL library compiled with own symbols in project properties or defined by #define SYMBOL syntax.

To select start screen you can use App constructor and Setup.CreateApp method

1) Create enum with platform options in your PCL

public enum OS
{
  Droid, Touch, WinPhone, WinStore, Mac, Wpf
}

2) Use this enum value in your App class constructor

public App (OS os)
{
    _os = os;
}

public override void Initialize()
{
    /*...*/

    switch(_os)
    {
        case OS.Touch:
            RegisterAppStart<ViewModels.FirstViewModel>(); break;
        default:
            RegisterAppStart<ViewModels.SearchViewModel>();
    }

    /*...*/
}

3) Pass current OS to App in Setup.CreateApp() method

{
    return new.Core.App(OS.Droid)
}