在Windows上使用UWP地理位置-API与Win32的应用程序10

问题描述:

我们正在构建一个多平台(桌面 - 和平板电脑的电脑)应用针对Windows 8.1以及10.我们处理空间数据,我们为用户提供可以使用设备的GPS接收器使用他当前的位置。为了方便适应(硬件)环境,我们将gps逻辑(包括API调用)放入基于配置加载的外部程序集中。在Windows上使用UWP地理位置-API与Win32的应用程序10

最近我们发现了一些问题,使用Windows 10中的Windows.Devices.Geolocation-API下的Geolocator(但以前在Windows 8.1上没有问题的情况下运行)的gps-module的问题,没有提供任何位置。进一步的调查和RTFM显示, - 在Windows 10下 - 我们*在调用位置之前调用RequestAccessAsync。

由于RequestAcessAsync-方法不可用在Windows 8.1,我们决定创建一个新的组件,针对Windows 10(再轻易被绑定/通过我们的配置中使用),什么工作得很好:

public Win10GpsProvider() 
{ 
    RequestAccessAsync(); 
} 

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

最多只运行到该被尽快RequestAccessAsync-方法被调用(在UI线程)抛出一个异常,指出该呼叫在一个应用程序容器的背景下进行。

该操作仅在一个应用程序容器的上下文中有效。 (来自HRESULT的例外:0x8007109A)

在台式机和平板电脑上均发生异常(通过远程调试进行验证)。

搜索多一点,增加了“位置”的要求的能力的package.appxmanifest可能会有帮助:

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

这就是我们实际上是停留在此刻:

  • 我们没有一个UWP-应用程序(其实不想/可以改变,因为我们瞄准赢8.1以及和具有包括设置规定展开工作流程)
  • 我们不能得到的装配运行无一例外,因为没有UWP a PP /背景

有没有办法让一个单独的程序为目标的的Windows 10版本的Windows.Devices.Geolocation-API的可以称得上/由Win32的应用程序加载?

据我所知,这是不可行的调用RequestAcessAsync方法,使其在Windows 8.1的应用程序工作。

但如果你直接在Windows 8.1项目中使用Geoposition,并且在Windows上运行此Windows 8.1的应用程序,例如在桌面上10设备,也将显示许可请求对话框:

enter image description here

所以有如果您不在Windows 8.1应用程序中调用RequestAcessAsync方法,应该没问题。

但如果用户选择“否”,在此权限请求对话框,我们可以捕获该异常,并启动设置页面,迫使用户启用该应用程序,例如像这样的位置设置:

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")); 
    } 
} 

Windows 8.1项目中的这段代码在Windows 8.1设备和Windows 10设备上都能正常工作。我不明白什么是你的Win32应用程序?