在Windows Universal App中检测当前设备 [英] Detecting current device in Windows Universal App

查看:58
本文介绍了在Windows Universal App中检测当前设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发布VS 2013 Update 2并构建一个示例通用应用程序.

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

我已经创建了一个用户控件,并且在两个MainPages上都添加了GridViews(在Windows Phone和Windows 8上).

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

当应用程序在Windows Phone上运行时,我想通过代码来更改某些内容.

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
{
}

推荐答案

通常,在构建应用程序时,可以使用预处理程序指令.在为Windows Phone构建应用程序时,默认情况下VS定义 WINDOWS_PHONE_APP (请查看项目属性"->构建"->条件编译"符号).因此,在代码中的任何地方都可以放置这样的语句:

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

更多信息,您可以在MSDN上 .

More information you can get at MSDN.

我建议您使用这种方法,因此在大多数情况下,您确切知道何时将特定代码用于Phone(ARM)或其他平台.当然,如果需要,可以为特定的构建配置/平台定义更多符号.

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.

备注:从W10开始,您需要在运行时中检查平台,然后可以使用

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

这篇关于在Windows Universal App中检测当前设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆