Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin表单地理定位任务已取消异常_Xamarin_Xamarin.android_Xamarin.forms - Fatal编程技术网

Xamarin表单地理定位任务已取消异常

Xamarin表单地理定位任务已取消异常,xamarin,xamarin.android,xamarin.forms,Xamarin,Xamarin.android,Xamarin.forms,我正在使用android、UWP和windows8项目开发Xamarin表单应用程序。我正在使用由Jamesmontemagno创建的地理定位插件来获取当前设备的位置。它在Windows8和UWP中运行良好,但每当我试图在android设备上运行它时,我总是收到任务取消异常。我已经按照建议检查了所有需要的权限,但仍然没有成功。我访问位置的代码如下 protected override void OnAppearing() { var定位器=CrossGeolocator.Current; loc

我正在使用android、UWP和windows8项目开发Xamarin表单应用程序。我正在使用由Jamesmontemagno创建的地理定位插件来获取当前设备的位置。它在Windows8和UWP中运行良好,但每当我试图在android设备上运行它时,我总是收到任务取消异常。我已经按照建议检查了所有需要的权限,但仍然没有成功。我访问位置的代码如下

protected override void OnAppearing()
{
var定位器=CrossGeolocator.Current;
locator.DesiredAccuracy=100;//100是新的默认值
if(locator.IsGeolocationAvailable&&locator.IsGeolocationEnabled)
{
尝试
{
var position=locator.GetPositionAsync(timeout毫秒:60000);
//var pp=helper.Setting.Location;
变量纬度=位置纬度;
var经度=位置经度;
}
捕获(例外情况除外)
{
var exc=ex;
}
}
}
下面是我的android清单设置的图片

多亏了@Radinator,以下是有效的解决方案

protected async override void OnAppearing()
{
var定位器=CrossGeolocator.Current;
locator.DesiredAccuracy=100;//100是新的默认值
if(locator.IsGeolocationAvailable&&locator.IsGeolocationEnabled)
{
尝试
{
等待设置位置();
}
捕获(例外情况除外)
{
var exc=ex;
}
}
}
专用异步任务SetLocation()
{
var定位器=CrossGeolocator.Current;
locator.DesiredAccuracy=100;//100是新的默认值
if(locator.IsGeolocationAvailable&&locator.IsGeolocationEnabled)
{
尝试
{
var position=await locator.GetPositionAsync(timeout毫秒:60000);
变量纬度=位置纬度;
var经度=位置经度;
}
捕获(例外情况除外)
{
//log-ex;
掷骰子;
}
}
}

多亏了@Radinator,以下是有效的解决方案

protected async override void OnAppearing()
{
var定位器=CrossGeolocator.Current;
locator.DesiredAccuracy=100;//100是新的默认值
if(locator.IsGeolocationAvailable&&locator.IsGeolocationEnabled)
{
尝试
{
等待设置位置();
}
捕获(例外情况除外)
{
var exc=ex;
}
}
}
专用异步任务SetLocation()
{
var定位器=CrossGeolocator.Current;
locator.DesiredAccuracy=100;//100是新的默认值
if(locator.IsGeolocationAvailable&&locator.IsGeolocationEnabled)
{
尝试
{
var position=await locator.GetPositionAsync(timeout毫秒:60000);
变量纬度=位置纬度;
var经度=位置经度;
}
捕获(例外情况除外)
{
//log-ex;
掷骰子;
}
}
}

尝试使用
wait
关键字,就像在以下应用程序中一样:

试试看
{
var定位器=CrossGeolocator.Current;
locator.DesiredAccuracy=50;
var position=await locator.GetPositionAsync(TimeOutMillimes:10000);
Console.WriteLine(“位置状态:{0}”,Position.Timestamp);
Console.WriteLine(“位置纬度:{0}”,位置纬度);
Console.WriteLine(“位置经度:{0}”,位置经度);
}
捕获(例外情况除外)
{
WriteLine(“无法获取位置,可能需要增加超时:”+ex);
}

这应该注意不存在争用条件,因此
任务取消异常

尝试使用
等待
关键字,就像在:

试试看
{
var定位器=CrossGeolocator.Current;
locator.DesiredAccuracy=50;
var position=await locator.GetPositionAsync(TimeOutMillimes:10000);
Console.WriteLine(“位置状态:{0}”,Position.Timestamp);
Console.WriteLine(“位置纬度:{0}”,位置纬度);
Console.WriteLine(“位置经度:{0}”,位置经度);
}
捕获(例外情况除外)
{
WriteLine(“无法获取位置,可能需要增加超时:”+ex);
}

这应该注意不存在竞态条件,因此,对于任何其他人来说,即使只有在Android上使用了Wait
,也会有超时,并且即使该设备的Google Maps应用程序工作正常,您也可能会遇到仅在某些安卓设备上发生的情况,但是他们中的很多人都是这样

这是一个谷歌从未解决过的老问题。解决方案,也是谷歌地图应用程序运行良好的一个可能原因,是使用谷歌Play服务的融合位置提供商


目前Geolocator插件只使用常规的Android位置提供程序,但James提到他希望在某个时候使用融合的提供程序。不过我自己还没有尝试过这个融合的提供商。

对于任何其他人来说,即使使用了
wait
,也只能在Android上使用,而且即使该设备的Google Maps应用程序工作正常,你也可能会遇到只有在某些Android设备上才会出现的问题,但请退出