GetPositionAsync在android中运行,但在IOS 11中不起作用

时间:2017-11-05 19:56:32

标签: ios xamarin.forms

System.Threading.Tasks.TaskCanceledException: A task was canceled.

2017-11-05 14:46:31.228 Project.iOS[36807:4480015] 
Unhandled Exception:
System.Threading.Tasks.TaskCanceledException: A task was canceled.
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00020] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.2.0.11/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:179 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.2.0.11/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.2.0.11/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`

当我尝试在ios.11模拟器上进行调试时,我很难处理以下代码抛出的异常,但此代码在android中运行良好。

private async Task<Plugin.Geolocator.Abstractions.Position> GetPositionAsync() {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 50;
            TimeSpan ts = new TimeSpan((Int32)10000);
            _position = await locator.GetPositionAsync(timeout: ts);
            return _position;
        } 

我已按照与@jamesmontemagno / Xamarin.Plugins / Geolocator /相关的各种网站中提到的所有说明进行操作,但没有运气。我确保我的info.plist包含以下代码:

  <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
  <string>This app needs access location when open and in the background.</string>
  <key>NSLocationAlwaysUsageDescription</key>
  <string>Need location for geolocator plugin.</string>
  <key>RequestWhenInUseAuthorization</key>
  <string>Need location for geolocator plugin.</string>
  <key>NSLocationWhenInUseUsageDescription</key>
  <string>Need location for geolocator plugin.</string> 

我想要实现的是通过Xamarin.Froms在所有三个平台(IOS,Android,Windows)上获取当前位置。 Android工作正常,但IOS是我遇到困难的地方。任何类型的帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

在与官方样本进行比较后,我发现问题是您设置的timeout参数对于任务完成而言太短,因此每次都取消。

解决方案:

await locator.GetPositionAsync(TimeSpan.FromSeconds(10));