Detect which APIs are present in UWP app

回眸只為那壹抹淺笑 提交于 2019-12-06 08:59:07

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.

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

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