【发布时间】:2015-11-26 05:41:52
【问题描述】:
Windows Universal(Windows 10 应用)中用于地理定位的新 API 提供了一种允许访问用户位置的新方法。
从 Windows 10 开始,在访问用户位置之前调用 RequestAccessAsync 方法。此时,您的应用必须在前台,并且必须从 UI 线程调用 RequestAccessAsync。
我在 UI 线程上运行一些非常简单的地理定位代码,如下所示,但我每次都“拒绝”位置权限,并且没有提示允许位置权限。有没有其他人遇到过这个? 如何在 Windows 10 应用中获得允许位置权限的提示?
地理定位方法
private async Task<ForecastRequest> GetPositionAsync()
{
try
{
// Request permission to access location
var accessStatus = await Geolocator.RequestAccessAsync();
if (accessStatus == GeolocationAccessStatus.Allowed)
{
// Get cancellation token
_cts = new CancellationTokenSource();
CancellationToken token = _cts.Token;
// If DesiredAccuracy or DesiredAccuracyInMeters are not set (or value is 0), DesiredAccuracy.Default is used.
Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = _desireAccuracyInMetersValue };
// Carry out the operation
_pos = await geolocator.GetGeopositionAsync().AsTask(token);
return new ForecastRequest()
{
Lat = (float)_pos.Coordinate.Point.Position.Latitude,
Lon = (float)_pos.Coordinate.Point.Position.Longitude,
Unit = Common.Unit.us
};
}
else
throw new Exception("Problem with location permissions or access");
}
catch (TaskCanceledException tce)
{
throw new Exception("Task cancelled" + tce.Message);
}
finally
{
_cts = null;
}
}
它的名字:
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ForecastViewModel vm = await ForecastViewModel.BuildViewModelAsync(await GetPositionAsync());
DataContext = vm.Forecast;
uxForecastList.Visibility = Visibility.Visible;
}
【问题讨论】:
-
您是否在应用清单中添加了“位置”作为一项功能?
标签: c# xaml geolocation win-universal-app uwp