Forum Xamarin.Forms

Device.OS returns value as Windows in 'Windows Phone Emulator' instead of WinPhone

I am developing for Windows Phone in Visual Studio 2013 Premium 4.5 using Xamarin.Forms 1.4.2. In some places for platform specific feature I am using conditional statements as below:

if (Device.OS == TargetPlatform.Android)
{
        //Similarly for iOS & WinPhone
}

If I run my project in Android or iOS emulator, corresponding platforms are detected properly, but when I run my project in Windows Phone emulator, instead of WinPhone it gives target platform as Windows.

Why so?

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @SubhaMuralidharan try to open a bug in bugzilla. There, Xamarin Team answer faster than on forums. And this seems a bug..
    Attach a simple project to bugzilla post so they can try exactly which is the problem.

  • SubhaMuralidharanSubhaMuralidharan USMember ✭✭

    @AlessandroCaliaro Thanks for the suggestion. Done :)

  • SubhaMuralidharanSubhaMuralidharan USMember ✭✭
    edited July 2015

    @JohnHardman Oh yeah, I am developing for Windows 8.1, Thanks I didn't read the documentation properly. But I have one more doubt. In some places I am using below code to detect platform.

    tag.FontSize = Device.OnPlatform(
                        iOS:      Device.Idiom == TargetIdiom.Tablet ? 14 : 10,
                        Android:  11,
                        WinPhone: 11
                    );
    

    In above case Windows property doesn't exist, so how do I specify FontSize for Windows Phone?

  • JohnHardmanJohnHardman GBUniversity mod

    @SubhaMuralidharan - I'll be doing this next week, but my initial thought is that for the simplest cases Device.OnPlatform will get replaced by if / elseif / elseif / else, doing a test on Device.OS. To do a direct replacement for Device.OnPlatform, it may be useful to create a simple class containing a method similar to OnPlatform.

    In some situations, there will be a secondary test on some combination of idiom, orientation and display size, but the initial bit would be testing Device.OS .

    Regards,

    John H.

  • JohnHardmanJohnHardman GBUniversity mod

    @SubhaMuralidharan - Here's my version of OnPlatform, to cope with Windows as well as WinPhone. I can imagine this getting extended in future, but this does the job for now.

    public static class DeviceWrapper
    {
        public static T OnPlatform<T>(T iOSValue, T androidValue, T winPhoneValue, T windowsValue)
        {
            switch (Device.OS)
            {
                case TargetPlatform.iOS:
                    return iOSValue;
                case TargetPlatform.Android:
                    return androidValue;
                case TargetPlatform.WinPhone:
                    return winPhoneValue;
                case TargetPlatform.Windows:
                    return windowsValue;
                default:
                    throw new ArgumentException("Unexpected platform detected");
            }
        }
    
    } // public static class DeviceWrapper
    
  • batmacibatmaci DEMember ✭✭✭✭✭

    @JohnHardman said:
    Are you building for Windows Phone 8.1? If so, the notes at http://developer.xamarin.com/guides/cross-platform/xamarin-forms/windows/controls/ say the following - the bit in brackets is the important bit:

    "Device.OS will be set to one of the values of TargetPlatform:
    •iOS
    •Android
    •WinPhone
    •Windows (new for Windows support, returned for BOTH Windows 8.1 and Windows Phone 8.1)"

    If you are building for Windows Phone 8.0, you should get WinPhone, but building for Windows Phone 8.1 gives Windows.

    Having finally got Windows 8.1 and Windows Phone 8.1 working, I am about to check everywhere I use the Device class, to do what is appropriate in terms of OS and Idiom (plus orientation and width/height in some cases).

    Regards,

    John H.

    UWP is also returning windows. I have package works for UWP but not wor Wp 8.1 how can I distinguish them?

  • JohnHardmanJohnHardman GBUniversity mod

    @batmaci - I posted some code for this a while back - it's on another thread at https://forums.xamarin.com/discussion/61139/a-question-about-uwp-app-for-xamarin-forms

  • batmacibatmaci DEMember ✭✭✭✭✭

    I later found the solution. you can combine Device.OS with Device.Idiom == TargetIdiom.Desktop. UWP is the only platform returns Desktop.

  • JohnHardmanJohnHardman GBUniversity mod

    @batmaci - WinRT 8.1 will return TargetIdiom.Desktop as well.

Sign In or Register to comment.