2016-08-19 98 views
1

我們正在構建一個多平臺(桌面 - 和平板電腦的電腦)應用針對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的應用程序加載?

回答

0

據我所知,這是不可行的調用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應用程序?

相關問題