是否有正确/推荐的方法来检测我在手机上运行的 UWP 应用程序? [英] Is there a correct/recommended way of detecting my UWP app I'm running on a Phone?

查看:27
本文介绍了是否有正确/推荐的方法来检测我在手机上运行的 UWP 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 JavaScript 试用 Windows 通用应用程序我注意到 WinJS.Utilities.isPhone 属性不再可用,这是有道理的,因为没有理由在运行时要求它.

Trying out Windows Universal apps with JavaScript I noticed the WinJS.Utilities.isPhone property is no longer available, which makes sense since there would be no reason to ask for that at runtime.

我确实想知道是否有适当的方法来检测我的应用在其中运行的设备,但仅用于测试目的.

I do want to know just for testing purposes if there is a proper way of detecting the device my app is running in.

我的问题与检测移动浏览器无关.我说的是一个全新的适用于 Window 10 的通用 Windows 应用程序,它可以在手机、台式机、平板电脑、Xbox、HoloLEns、物联网设备等上运行.WinJS 有一个属性可以告诉我我是否在手机上运行.那个财产现在没有了.由于与检测移动浏览器"重复,请不要关闭此问题.那不是我需要的.

My question is NOT about detecting a mobile browser. I'm talking about about a brand new Universal Windows App for Window 10 that can run on phones, desktops, tablets, Xbox, HoloLEns, IoT devices et all. WinJS had a property that would tell me whether I was running on the phone or not. That property is now gone. Please don't close this question due to duplicate with "detecting mobile browser". That is NOT what I need.

推荐答案

警告:由于硬件的动态特性,任何形式的设备检测都是脆弱的 - 明天可能会出现打破应用逻辑的新设备.最好将这些 API 仅用于遥测/分析,而不是触发运行时行为.

通常情况下,您真正​​想知道的是设备或应用的某些属性固有地绑定到设备系列(此设备是否支持 SystemTray代码> API?是否附加了键盘?窗口宽度是否超​​过 500 像素?等等).

More often than not, what you really want to know is some attribute of the device or the app that is not inherently tied to the device family (does this device support SystemTray API? Is there a keyboard attached? Is the window more than 500px wide? etc.).

也就是说,在 Windows 10 中,您可以通过 AnalyticsInfo.VersionInfo.DeviceFamily 它会告诉你诸如移动"或桌面"或Xbox"等(其中移动"可以是任何类型的设备 - 手机、平板电脑等).还有一个 DeviceForm 属性 可能 有帮助,但同样,您不能在运行时真正依赖它来确定性地说您正在手机"上运行.

That said, in Windows 10 you can query the DeviceFamily via AnalyticsInfo.VersionInfo.DeviceFamily and it will tell you things like "Mobile" or "Desktop" or "Xbox" etc. (where "Mobile" could be any class of device - phone, tablet, etc.). There is also a property DeviceForm that might be helpful, but again you can't really rely on it at runtime to deterministically say you're running on "a phone."

所以基本上答案是您可以使用这些 API 进行遥测,但不要将任何值硬编码到您的应用程序中,以免在新设备上市时它会崩溃."至少,始终确保您处理返回值不是您所知道的先验值的情况.

So basically the answer is "you can use these APIs for telemetry but don't hard-code any values into your app lest it break when a new device arrives on the market." At the very least, always make sure you handle the case where the returned value isn't one you know about a-priori.

这篇关于是否有正确/推荐的方法来检测我在手机上运行的 UWP 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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