Check APIs availability and version

Compilation directives

Do not use to check availability!!!

#if WINDOWS_PHONE_APP
  Windows.Phone.UI.Input.HardwareButtons.BackPressed += this.HardwareButtons_BackPressed;
#endif

Available Contants

Use it only for shared projects!!!

  • WINDOWS_APP
  • WINDOWS_PHONE_APP
  • WINDOWS_UAP

Test for capabilities

Windows.Foundation.Metadata.ApiInformation

  • IsApiContractPresent
  • IsEnumNamedValuePresent
  • IsEventPresent
  • IsMethodPresent
  • IsPropertyPresent
  • IsReadOnlyPropertyPresent
  • IsTypePresent
  • IsWritablePropertyPresent

C#-Code

Hardware Buttons

var api = "Windows.Phone.UI.Input.HardwareButtons";

if(Windows.Foundation.Metadata.ApiInformation.IsTypePresent(api))
{
  Windows.Phone.UI.Input.HardwareButtons.CameraPressed += CameraButtonPressed;
}

Statusbar

When the check for Windows.Foundation.Metadata.ApiInformation.IsTypePresent(„Windows.UI.ViewManagement.StatusBar“) does not work, use the check for Hardware Buttons.

if(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
{
  var i = Windows.UI.Input.ViewManagement.StatusBar.GetForCurrentView().ShowAsync();
}

Device Family

C#

Uses the Device Trigger

XAML

<Grid>
<VisualStateManager.VisualStateGroups>
 <VisualStateGroup>
  <VisualState>
   <VisualState.StateTriggers>
    <t:DeviceFamilyTrigger DeviceFamily="Desktop" />
   <VisualState.StateTriggers>
   <VisualState.Setters>
    <Setter Target="StatusBarControls.Visibility" Value="Collapsed" />
   </VisualState.Setters>
  </VisualState>
 </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
...
</Grid>

 

Test for API Version

var contract = "Devices.Scanners.ScannerDeviceContract";
int majorVersionRequired = 3;
if(Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent(contract, majorVersionRequired))
{
  // Call API that is present in V3 and above
}
else
{
  // Call Original code / Old Version
}