Determine which APIs are present in the UWP app.

Understanding why it is not a good idea to define a device type to perform functionality at runtime, best practices dictate discovering which APIs are present. This way, users launching their tablet in desktop mode, for example, won't experience unwanted behavior. Also, because the hardware is so dynamic, checking for user interaction such as touch is not a good approach either.

In our project, we decided to identify the APIs that we will need for three different screens - small, medium and large. Microsoft has listed these APIs here . But the list is quite long and it would be cumbersome to check for each one.

Any suggestions for performing these checks without repeating calls like this one ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")

for every contract in the list of APIs provided by Microsoft would be greatly appreciated.

Thanks in advance.

+1


source to share


1 answer


What you usually do is just add a check for code that requires a specific API:

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

      

If you know that you have multiple blocks of code that require the same API, you can cache that value.

An alternative is to check the entire complete contract at once. If you know you need to be able to make phone calls, instead of checking every event or method call, just check the contract.



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

      


Not knowing what your client might need is not a problem. The answer to this problem is YAGNI . Do not check the contract if you do not intend to implement it.

+1


source







All Articles