简体   繁体   中英

How to check if Windows device is phone or tablet/pc?

How to check if Windows device is phone or tablet/PC? I need to know in order to use different sizes for xaml elements

I think you can try to use EasClientDeviceInformation class:

EasClientDeviceInformation info = new EasClientDeviceInformation();
string system = info.OperatingSystem;

on WP it should return WindowsPhone and Windows on desktop.

You shouldn't use different sized Xaml based on if it's a phone or a tablet. You should use different Xaml based on the size and resolution of the screen.

On Windows 8.1 there are enough Xaml differences that you may be stuck needing to do different things for Phone and PC/Tablet, but in that case you will generally use different Xaml files for the two projects. You can also two versions of a resource dictionary with phone and PC/Tablet specific styles and place them in the platform specific projects to get different behavior on each device.

If you really want to know this from code, on 8.1 you always use different binaries for phone and for tablet: you can just compile it in with compile-time constants:

bool IsAPhone()
{
#if WINDOWS_PHONE_APP
    return true;
#else
    return false;
#endif
}

On Windows 10 you'll use the same binary and the same Xaml API for both, and the same device may display on both a small (phone) screen and a large one.

While you can still use separate Xaml files to tailor the UI for different devices you can also use responsive techniques (Visual States and AdaptiveTriggers, RelativePanels, etc.) to reflow the UI for the window size so the same app can provide different but appropriate views depending on the size of the screen regardless of the device the app runs on.

Take a look at the Build session Design: UX Patterns and Responsive Techniques for Universal Windows Apps for more details.

    public static bool DeviceIsPhone()
    {
        Type StatusBarType = Type.GetType("Windows.UI.ViewManagement.StatusBar, Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime");
        if (StatusBarType != null)
            return true;
        return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM