Detect which APIs are present in UWP app

穿精又带淫゛_ 提交于 2020-01-02 09:42:33

问题


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.

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.

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.

Thanks in advance.


回答1:


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

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");

"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.




回答2:


Actually I'm developing an app for UWP for Mobile and Desktop devices and the contract "Windows.ApplicationModel.Calls.CallsPhoneContract" if you have skype installed on your desktop is gonna be a findable Api contract.

To really ensure you are just aiming to phone contract you should use this code:

ApiInformation.IsApiContractPresent("Windows.Phone.PhoneContract",1,0)

which is a exclusive contract for windows phone.

Also I add a proof of an screenshot taken from my debugging session proving that "Windows.ApplicationModel.Calls.CallsPhoneContract" can be found at desktop even thought in documentation isnt considered.

Also I just reported this issue to Microsoft Here at github and here at microsoft documentation

hope this answer help everyone, if you are building apps for desktop and mobile use this contract instead "Windows.Phone.PhoneContract".



来源:https://stackoverflow.com/questions/34342434/detect-which-apis-are-present-in-uwp-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!