Xamarin.ios iphone设备当前位置(街道地址)

时间:2016-10-13 09:53:22

标签: xamarin xamarin.ios

    public static async Task<Position> GetCurrentLocation()
    {
        try
        {

            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 100;


            if (!locator.IsGeolocationAvailable)
                throw new NotSupportedException("Geolocation not available");
            if (!locator.IsGeolocationEnabled)
                throw new GeolocationException(GeolocationError.PositionUnavailable);


            return await locator.GetPositionAsync(timeoutMilliseconds: 100000);
        }
        catch (Exception ex)
        {
            //TODO: Add error logging
            return null;
        }
    }
<{1>} return await locator.GetPositionAsync(timeoutMilliseconds: 100000); 线程退出和调试器不返回但输出窗口正确显示位置但未返回到代码。这次休息的可能原因应该是什么?

1 个答案:

答案 0 :(得分:0)

如果我理解您的问题,您想知道为什么您的异步代码的行为与它一样吗?

让我们首先获得正确的术语:退出no thread。 Async / await不是关于线程,而是关于编排异步操作。

如果您等待的await尚未完成,则您的代码会在Task返回。之后,将在awaited代码之后执行执行。在您的情况下,唯一会发生的事情是返回任务(=位置)的结果。

所以无论如何,你的方法都会退出:

  • 因为任务尚未完成
  • 或因为它已完成并且您正在使用return