Xamarin form Geolocation task cancelled exception

后端 未结 4 1679
旧时难觅i
旧时难觅i 2021-01-16 05:01

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

4条回答
  •  广开言路
    2021-01-16 05:27

    Try using the await keyword like it is used in the original code:

    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);
    }
    

    This should take care that there are no race condition and therefore TaskCancellationException.

提交回复
热议问题