检测UWP应用中存在哪些API [英] Detect which APIs are present in a UWP app

查看:51
本文介绍了检测UWP应用中存在哪些API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了解为什么不建议检测设备类型以执行运行时功能,因此最佳实践要求改为检测存在哪些API.这样,例如,在桌面模式下运行其平板电脑的用户将不会遇到不良行为.而且,由于硬件是如此动态,因此检查诸如触摸功能之类的用户交互也不是一种好方法.

Understanding why it's not recommended to detect the device type to perform run-time functionality, best practices dictate detecting which APIs are present instead. This way, users running their tablet in desktop mode, for instance, will not experience undesired behavior. Also, since hardware is so dynamic, checking for user interactions like touch capability is not a good approach either.

在我们的项目中,我们决定确定三种不同屏幕宽度(小,中和大)所需的API.Microsoft已在此处列出了这些API.但是此列表相当长,要检查每个列表的存在会很麻烦.

On our project, we have decided to identify the APIs we'll need for three different screen widths - small, medium, and large. Microsoft has listed these APIs here. But this list is rather long and checking for the presence of each of them would be cumbersome.

关于如何执行这些检查而不重复像Microsoft提供的API列表中的每个合同这样的 ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")的任何建议将不胜感激.

Any suggestions on how to perform these checks without repeating calls like this ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons") for each contract in the API list provided by Microsoft would be greatly appreciated.

推荐答案

您通常要做的只是添加对需要特定API的代码的检查:

What you usually do is just add the check around the code that needs the particular API:

if(ApiInformation.IsEventPresent("Windows.Phone.UI.Input.HardwareButtons", "BackPressed"))
{
    HardwareButtons.BackPressed += OnHardwareButtonsBackPressed;
}

如果您知道有多个需要相同API的代码块,则可以缓存该值.

If you know you got multiple code blocks requiring the same API, you can cache the value.

另一种方法是一次检查整个完整合同.如果您知道需要打个电话,而不是检查每个事件或方法调用,只需检查合同即可.

An alternative is to check a whole complete contract at once. If you know you need to be able to do phone calls, instead of checking each event or method call, just check on the contract.

ApiInformation.IsApiContractPresent("Windows.ApplicationModel.Calls.CallsPhoneContract");


不知道"您的客户可能需要的是非问题".该问题的答案是 YAGNI .除非您即将执行合同,否则不要检查合同.


"Not knowing" what your client might need is a 'non-issue'. The answer to that problem is YAGNI. Do not check on a contract unless you're at the point you're gonna implement it.

这篇关于检测UWP应用中存在哪些API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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