Using UWP Geolocation-API with Win32-Application on Windows 10

2

We're building a multi-platform (desktop- and tablet-computers) application targeting Windows 8.1 as well as 10. As we process spatial data, we offer the user the possibility to use his current location using the device's gps receiver. To allow easy adaption to the (hardware) environment, we placed the gps-logic (including the API-calls) into an external assembly that we load based on a configuration.

Recently we discovered some problems with the gps-module using the Geolocator from Windows.Devices.Geolocation-API under windows 10 (but previously running without problem on Windows 8.1), not delivering any location. Further investigation and RTFM showed up, that - under Windows 10 - we were obliged to call the RequestAccessAsync before calling for the location.

As the RequestAcessAsync-method isn't available in Windows 8.1, we decided to create a new assembly, targeting Windows 10 (and then easily being bound/used through our configuration), what worked quite well:

public Win10GpsProvider()
{
    RequestAccessAsync();
}

public async void RequestAccessAsnyc()
{
    //next line causes exception:
    var request = await Geolocator.RequestAccessAsync();
    switch (request)
    {
        // we don't even get here... :(
    }
}

Only up to running into an exception that gets thrown as soon as the RequestAccessAsync-method is being called (in UI-thread), stating that the call has to be made in the context of an app container.

This operation is only valid in the context of an app container. (Exception from HRESULT: 0x8007109A)

The exception occurs on both, desktop as well as tablet (verified through remoted debugging).

Searching a bit more, adding the "location" as a required capability to the package.appxmanifest may help:

<Capabilities>
    <Capability Name="location"/>
</Capabilities>

That's where we're actually stuck at the moment:

  • We don't have an UWP-application (and actually don't want/can change that, as we're targeting Win 8.1 as well and have a defined deployment workflow including setups)
  • We can't get the assembly run without exception as there is no UWP app/context

Is there a way to get a separate assembly that targets the Windows 10 version of the Windows.Devices.Geolocation-API and can be called/loaded by a Win32-application?

c#
.net
winapi
windows-10
uwp
asked on Stack Overflow Aug 19, 2016 by Gr33z00

1 Answer

0

AFAIK, it is not doable to call RequestAcessAsync method and make it work in Windows 8.1 app.

But if you directly use Geoposition in windows 8.1 project, and run this windows 8.1 app on windows 10 device for example on desktop, the permission request dialog will also be displayed:

enter image description here

So there should be no problem if you don't call RequestAcessAsync method in windows 8.1 app.

But in case user choose "No" in this permission request dialog, we can catch the exception and launch the Setting page to force user to enable the Location setting for this app for example like this:

private Geolocator _geolocator = null;
private uint _desireAccuracyInMetersValue = 0;

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    try
    {
        var _cts = new CancellationTokenSource();
        CancellationToken token = _cts.Token;
        _geolocator = new Geolocator { DesiredAccuracyInMeters = _desireAccuracyInMetersValue };
        Geoposition pos = await _geolocator.GetGeopositionAsync().AsTask(token);

        // Subscribe to PositionChanged event to get updated tracking positions
        _geolocator.PositionChanged += OnPositionChanged;

        // Subscribe to StatusChanged event to get updates of location status changes
        _geolocator.StatusChanged += OnStatusChanged;
    }
    catch (Exception ex)
    {
        await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-location"));
    }
}

This code in windows 8.1 project works fine both on windows 8.1 device and windows 10 device. And I don't quite understand what is your Win32-application for here?

answered on Stack Overflow Aug 24, 2016 by Grace Feng

User contributions licensed under CC BY-SA 3.0