How to know the current OS / platform of the executing code (Android / iOS)

后端 未结 3 1847
南方客
南方客 2021-01-01 09:51

Using Xamarin.Android and Xamarin.iOS, I need to now the current OS in a shared code section. Be it an enum, an int or a string, it doesn\'t matter.

I tried this:

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-01 10:45

    I'm using DeviceInfo plugin. It supports Android, iOs, Windows Phone Silverlight, Windows Phone RT, Windows Store RT, Windows 10 UWP and it works perfectly.

    It can be used in PCL like this (you must add the plugin to the platform projects and to PCL):

    var platform = CrossDeviceInfo.Current.Platform;
        switch(platform){
            case Platform.iOS:
                // iOS
                break;
            case Platform.Android:
                // Android
                break; 
        }
    

    This kind of code isn't a good practice in PCL, its better to use Dependency Injection (this plugin use that) to customize platform specific behaviour.

提交回复
热议问题