在Windows Universal App中检测当前设备

编程入门 行业动态 更新时间:2024-10-27 14:28:14
本文介绍了在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开始,您需要在运行时中检查平台,然后可以使用 ApiInformation类,并检查api中是否存在特定类型.例如这样的

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中检测当前设备

本文发布于:2023-11-06 04:16:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562692.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设备   Windows   Universal   App

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!