Geolocator.RequestAccessAsync 抛出 "method called at unexpected time" 异常
Geolocator.RequestAccessAsync throws "method called at unexpected time" exception
我有一段代码用于获取设备位置。这是在 monogame windows uwp 项目中完成的。以下代码之前有效(我在 VS2015 社区)。我最近做了一个新的 OS 并安装了 VS2017 Community。我终于得到了我的项目来构建和 运行。唯一不起作用的是 Geolocator.RequestAccessAsync 抛出 "method called at unexpected time" 异常。有什么想法吗?
public async Task<Vector2> GetDeviceGps()
{
var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
case GeolocationAccessStatus.Allowed:
// If DesiredAccuracy or DesiredAccuracyInMeters are not set (or value is 0), DesiredAccuracy.Default is used.
Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = 10 };
// Subscribe to the StatusChanged event to get updates of location status changes.
//geolocator.StatusChanged += OnStatusChanged;
// Carry out the operation.
Geoposition pos = await geolocator.GetGeopositionAsync();
return new Vector2((float)pos.Coordinate.Point.Position.Longitude, (float)pos.Coordinate.Point.Position.Latitude);
case GeolocationAccessStatus.Denied:
//Do something!
break;
case GeolocationAccessStatus.Unspecified:
//Murp
break;
}
return Vector2.Zero;
}
这是这样调用和处理的(从游戏的更新方法调用):
mapper.GetDeviceGps().ContinueWith(pos =>
{
Vector2 bob = pos.Result;
});
当您第一次在您的应用程序中 运行 RequestAccessAsync 时,系统会要求用户授予您的应用程序位置权限。因此该方法需要在UI线程中执行。
我有一段代码用于获取设备位置。这是在 monogame windows uwp 项目中完成的。以下代码之前有效(我在 VS2015 社区)。我最近做了一个新的 OS 并安装了 VS2017 Community。我终于得到了我的项目来构建和 运行。唯一不起作用的是 Geolocator.RequestAccessAsync 抛出 "method called at unexpected time" 异常。有什么想法吗?
public async Task<Vector2> GetDeviceGps()
{
var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
case GeolocationAccessStatus.Allowed:
// If DesiredAccuracy or DesiredAccuracyInMeters are not set (or value is 0), DesiredAccuracy.Default is used.
Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = 10 };
// Subscribe to the StatusChanged event to get updates of location status changes.
//geolocator.StatusChanged += OnStatusChanged;
// Carry out the operation.
Geoposition pos = await geolocator.GetGeopositionAsync();
return new Vector2((float)pos.Coordinate.Point.Position.Longitude, (float)pos.Coordinate.Point.Position.Latitude);
case GeolocationAccessStatus.Denied:
//Do something!
break;
case GeolocationAccessStatus.Unspecified:
//Murp
break;
}
return Vector2.Zero;
}
这是这样调用和处理的(从游戏的更新方法调用):
mapper.GetDeviceGps().ContinueWith(pos =>
{
Vector2 bob = pos.Result;
});
当您第一次在您的应用程序中 运行 RequestAccessAsync 时,系统会要求用户授予您的应用程序位置权限。因此该方法需要在UI线程中执行。