Category: Windows

Determine if running on Windows Phone from a UAP application

There is new metadata query support in Windows 10 to allow your code to react at runtime based on which APIs are available. For example if running on a phone device you may want to start a call, and offer alternative actions on a desktop PC. The correct way to check the availability is to use the ApiInformation class which has a number of methods which take the string name of the type or method and return a Boolean. Without delving into the question of whether a string is the correct approach lets look at an alternative issue.

You want to determine the device type you are running on – say Windows, Windows Phone, Xbox etc to change the type of content you display. You want this high level information not just to alter screen layout or use a specific API. Each Extension SDK provides the device specific APIs for the target devices. Currently the preview tools ship with Windows Desktop Extension SDK and Windows Mobile Extension SDK. If you look at the Object Browser in Visual Studio you can see that adding each of these adds additional root level entries (like a new DLL) and each named after a Contract which is a type contained within it alongside the associated APIs. So you want to quickly determine if you are running on a “Windows Phone” device you could use the following code:-

bool isPhone = ApiInformation.IsApiContractPresent("Windows.Phone.PhoneContract",1);

It’s not currently clear exactly which contracts will be present on which devices – the APIs provided by the Windows.Phone.PhoneContract lib are all familiar APIs present from previous versions of Windows Phone. Since “Windows Mobile” will also be the SKU used for small tablets it isn’t clear if these will also implement this contract or not. If no extension SDKs are added all UAP apps will support Windows.Foundation.FoundationContract and Windows.Foundation.UniversalApiContract.

Although not a part of the current preview tools one can expect a similar set of contracts for the other flavours of Windows – Xbox, IoT etc

Moving Forwards: Geolocation

Recently I open-sourced a number of Compact Framework projects and when I was working on re-writing an application which used them as a Windows Runtime app I started to think about how much of the code might be useful for app projects. Obviously the API surface for Windows Runtime is totally different to the .NET Compact Framework. In some cases functionality which I wrote is built into the runtime, in other cases I’d made use of P/Invoke to call native APIs which is not an option. However one thing I noticed was that the Windows.Devices.Geolocation namespace which supersedes System.Device.Location was missing one key feature – a built in method to determine the distance between two points. Since I had code for this which was using the same method as the .NET framework I thought this was a prime candidate for migration.

The Windows Runtime is a rather more complex API and whereas .NET had a single GeoCoordinate type the runtime has BasicGeoposition (containing raw latitude, longitude etc) and a number of other types – Geocoodinate, Geocircle, Geopoint etc I decided to first implement an extension method which would work with two BasicGeopositions and then as a helper added an extension method for Geocoordinate which used the logic from the first.

The methods now live in a new NuGet package currently supporting Windows 8.1 and Windows Phone 8.1 – Charming Geolocation and the code will be available in the Charming project. Just add:-

using InTheHand.Devices.Geolocation;

and you can use the GetDistanceTo methods.

Part of the project using this code required an iOS implementation too for which I’ve used Xamarin. Anyone who has used this will know that while the location functionality is similar to what we are familiar with on Windows the API is significantly different. Because I wanted to fix that just the once too I wrote a wrapper API to expose the Windows API wrapping all the CoreLocation stuff. I’ll be adding this library to the NuGet package shortly and am also in the process of moving all the Charming code from CodePlex to GitHub too.