Xamarin表格地理定位任务已取消异常 [英] Xamarin form Geolocation task cancelled exception

查看:117
本文介绍了Xamarin表格地理定位任务已取消异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Andorid,UWP和Windows 8项目开发Xamarin表单应用程序.我正在使用Jamesmontemagno创建的Geolocation插件来获取当前设备位置.它在Windows 8和UWP中工作正常,但是每当我尝试在android设备上运行它时,我都会不断收到任务取消的异常.我已经按照建议检查了所有必需的权限,但是仍然没有运气.我访问位置的代码如下

I am working on Xamarin form app with andorid, UWP and Windows 8 project. I am using Geolocation plugin created by Jamesmontemagno to get the current device location. It is working fine in windows 8 and UWP but whenever I am trying to run it against the android device I keep getting task cancelled exception. I have checked all the permissions that are required as per suggestion but still no luck. My code to access location is below

  protected override void OnAppearing()
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 100; //100 is new default
            if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
            {
                try
                {
                    var position = locator.GetPositionAsync(timeoutMilliseconds: 60000).Result;
                    //var pp = helper.Setting.Location;
                    var Latitude = position.Latitude;
                    var Longitude = position.Longitude;
                }
                catch(Exception ex)
                {
                    var exc = ex;
                }
            }
        }

下面是我的Android清单设置的图片

Below is an image for my settings for android manifest

推荐答案

感谢下面的@Radinator是有效的解决方案.

Thanks to @Radinator below is the working solution.

protected async override void OnAppearing()
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 100; //100 is new default
            if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
            {
                try
                {
                    await SetLocation();
                }
                catch (Exception ex)
                {
                    var exc = ex;
                }
            }
        }

        private async Task SetLocation()
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 100; //100 is new default
            if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
            {
                try
                {
                    var position = await locator.GetPositionAsync(timeoutMilliseconds: 60000);

                    var Latitude = position.Latitude;
                    var Longitude = position.Longitude;
                }
                catch (Exception ex)
                {
                    //log ex;
                    throw ex;
                }
            }
        }

这篇关于Xamarin表格地理定位任务已取消异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆