C# UWP: Checking for Platform Extensions at Runtime

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Universal Windows Platform (UWP) Apps

Windows 8.1 introduced Universal Windows 8 apps, which allowed you to develop apps that target both Windows and Windows Phone from a shared codebase. Windows 10, on the other hand, introduces UWP apps and the Universal Windows Platform (UWP), which provide a guaranteed core API layer across devices.

UWP provides useful building blocks that make it easier to design apps for multiple device families. For example, UWP provides a set of universal controls that are guaranteed to work well on all Windows-powered devices. This set of universal controls includes everything from common form controls, such as radio buttons and text boxes, to grid views and list views.

Universal Input

A UWP app can run on many different kinds of devices that have different forms of input, screen resolutions, DPI density, and other unique characteristics. Windows 10 provides new universal controls, layout panels, and tooling to help you adapt your UI to the devices your app may run on.

Some aspects of your app's UI will adapt automatically across devices. Controls such as buttons and sliders automatically adapt across device families and input modes. Your app's user-experience design, however, may need to adapt depending on the device on which the app is running.

Dynamically Detecting Features at Runtime in a UWP App

Windows.Foundation.Metadata Namespace

The Windows.Foundation.Metadata Namespace defines all the attributes that indicate fundamental properties of Windows Runtime types.

Windows.Foundation.Metadata.ApiInformation Class

The ApiInformation class enables you to detect whether a specified member, type, or API contract is present to safely make API calls across various devices.

Windows.Foundation.Metadata.ApiInformation Methods

IsApiContractPresent

Signifies if the API contract with the specified name and major version number is present. An API contract is simply a set of APIs. Logically related types are grouped into an API contract. An API contract represents a feature—a set of related APIs—that together deliver some particular functionality.

The following example determines, with the use of IsApiContractPresent, if the device can place phone calls. If it can, it checks to see if a phone line exists and that the number to dial is more than 0 characters. If all of these conditions exist, the app dials the supplied number; otherwise, it displays a message that informs the user that the call cannot be placed.

using Windows.Foundation.Metadata;
if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel
   .Calls.CallsPhoneContract", 1,0))
{
   if ((PhoneLine != null) && (PhoneNumber.Trim().Length > 0))
   {
      PhoneLine.Dial(PhoneNumber, DisplayName);
   }
   else
   {
      var dialog = new MessageDialog("Cannot make call");
      await dialog.ShowAsync();
   }
}

IsEnumNamedValuePresent

Signifies if a specified named constant is present for a specified enumeration.

In the following examples, it is easy to understand the reason this method exists. In the following examples, you will test to see if certain video formats are allowed with certain codecs.

2160p UHD format.

using Windows.Foundation.Metadata;
if (ApiInformation.IsEnumNamedValuePresent("Windows.Media
   .MediaProperties.VideoEncodingQuality", "Uhd2160p"))
{
}
try
{
   Uri path = new Uri("ms-appx:///AppVideos/Intro1.mp4");
   mediaPlayer.Source = MediaSource.CreateFromUri(path);
}
catch (Exception ex)
{
   if (ex is FormatException)
   {
      // Handle exception.
      // For example: Log error or notify user problem with file
   }
}

4320p UHD format.

using Windows.Foundation.Metadata;
if (ApiInformation.IsEnumNamedValuePresent("Windows.Media
   .MediaProperties.VideoEncodingQuality", "Uhd4320p"))
{
}
try
{
   Uri path = new Uri(path);
   mediaPlayer.Source = MediaSource.CreateFromUri(path);
}
catch (Exception ex)
{
   // Exception details
}

IsEventPresent

Signifies if a specified event is present for a specified type. An example of IsEventPresent is to determine whether or not the Back button is present on the device. Remember, you can run your apps on a desktop PC running Windows 10 as well.

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

This example checks to see if the BackPressed event is present on the device that the app is running on. Another example follows:

private bool CameraButtonExists =
   ApiInformation.IsEventPresent("Windows.Phone.UI.Input
      .HardwareButtons", "CameraPressed");

The preceding code determines if a camera is present on the device by checking if the CameraPressed event is available.

IsMethodPresent

Signifies if a specified method is present for a specified type.

The following code determines if the GetNetworkUsageAsync method inside the Windows.Networking.Connectivity.ConnectionProfile class exists. The Windows.Networking.Connectivity.ConnectionProfile class represents a network connection and it provides information about the connection status and connectivity statistics.

using Windows.Foundation.Metadata;
if (ApiInformation.IsMethodPresent("Windows.Networking
   .Connectivity.ConnectionProfile", "GetNetworkUsageAsync"))
{
   // Do work
}

IsPropertyPresent

Signifies if a specified property (writeable or read-only) is present for a specified type.

IsReadOnlyPropertyPresent

Signifies if a specified read-only property is present for a specified type.

IsTypePresent

Signifies whether a specified type is present.

using Windows.Foundation.Metadata;
if(ApiInformation.IsTypePresent("Windows.Media.Playlists
   .Playlist"))
{
   await FavoritePlaylist.SaveAsAsync( ... );
}

The previous code makes a quick runtime check for the presence of the Playlist class. If it exists, it references and calls the SaveAsAsync method on the class.

IsWriteablePropertyPresent

Signifies if a specified writeable property is present for a specified type.

Conclusion

Making use of the Extension SDK for UWP is very easy, as you can see. I have only covered a tiny bit, but you are now armed with enough knowledge to check for Platform extensions in big or small projects.



About the Author

Hannes DuPreez

Hannes du Preez has been a Microsoft MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978@gmail.com

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date