首页 文章

Xamarin表格地理位置任务取消了异常

提问于
浏览
1

我正在使用带有andorid,UWP和Windows 8项目的Xamarin表单应用程序 . 我正在使用由Jamesmontemagno创建的Geolocation插件来获取当前的设备位置 . 它在Windows 8和UWP中工作正常,但每当我试图对Android设备运行它时,我一直得到任务取消异常 . 我已根据建议检查了所有必需的权限,但仍然没有运气 . 我的访问位置的代码如下

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清单
enter image description here
的图像

4 回答

  • 3

    对于其他任何即使使用 await 暂停的人,只能在Android上使用,即使该设备的Google Map 应用程序运行正常,您可能会遇到this bug,这只会发生在某些Android设备上,但其中有不少就是这样 .

    问题是谷歌从未修复的旧问题 . 解决方案以及Google Map 应用正常运行的一个可能原因是使用Google Play服务的融合位置提供商 .

    目前Geolocator插件只使用常规Android位置提供程序,但James提到他希望在某些时候使用Fused提供程序 . 我自己还没试过融合的提供者 .

  • 3

    尝试使用 await 中使用的 await 关键字:

    try
    {
      var locator = CrossGeolocator.Current;
      locator.DesiredAccuracy = 50;
    
      var position = await locator.GetPositionAsync (timeoutMilliseconds: 10000);
    
      Console.WriteLine ("Position Status: {0}", position.Timestamp);
      Console.WriteLine ("Position Latitude: {0}", position.Latitude);
      Console.WriteLine ("Position Longitude: {0}", position.Longitude);
    }
    catch(Exception ex)
    {
      Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex);
    }
    

    这应该注意没有竞争条件,因此 TaskCancellationException .

  • 1

    感谢@Radinator下面是工作解决方案 .

    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;
                    }
                }
            }
    
  • 0

    面对v3.0.4中的“任务被杀”问题 . 以下对我有用:

    • 卸载应用程序

    • 将Geolocator更新为预发布4.0

相关问题