Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting current device in Windows Universal App

I am trying out released VS 2013 Update 2 and building a sample Universal Application.

I have created a user control and on both MainPages added GridViews (on Windows Phone and Windows 8).

I want to change some things via code when app is running on Windows Phone.

Is there a way to do something like:

if(<deviceType> == "WindowsPhone")
{
}
else
{
}
like image 653
Cheese Avatar asked Apr 24 '14 11:04

Cheese


2 Answers

Normally when building your app, you can use preprocessor directives. When building app for windows phone, VS as default defines WINDOWS_PHONE_APP (take a look at Project Properties -> Build -> Conditional compilation symbols). Therefore anywhere in your code you can put such a statement:

#if WINDOWS_PHONE_APP
    // do when this is compiled as Windows Phone App
#else
    // not for windows phoen
#endif

More information you can get at MSDN.

I would advise to use this approach, hence in most cases you know exactly when you will use specific code for Phone (ARM) or other platform. Of course if you need you can define more symbols for specific build configurations/platforms.

Remarks: Since W10, where you need to check the platform in Run-Time, then you can use ApiInformation class and check if specific type exists in the api. For example like this:

if (ApiInformation.IsApiContractPresent("Windows.Phone.PhoneContract", 1))
   // do code for mobile
else 
   // do code for other
like image 161
Romasz Avatar answered Sep 19 '22 17:09

Romasz


That's what worked for me in Universal Windows (Windows 10) project

public static Platform DetectPlatform()
{
    bool isHardwareButtonsAPIPresent =
        ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons");

    if (isHardwareButtonsAPIPresent)
    {
        return Platform.WindowsPhone;
    }
    else
    {
        return Platform.Windows;
    }
}
like image 45
duo Avatar answered Sep 18 '22 17:09

duo