public async Task Run(CancellationToken token) { await Task.Run(async() => { while (!stopping) { token.ThrowIfCancellationRequested(); try { await Task.Delay(2000); System.Diagnostics.Debug.Print($"间隔2秒GPS定位一次....."); var request = new GeolocationRequest(GeolocationAccuracy.Best); var location = await Geolocation.GetLocationAsync(request); if (location != null) { GlobalSettings.Latitude = location.Latitude; GlobalSettings.Longitude = location.Longitude; System.Diagnostics.Debug.Print($"{location.Latitude} {location.Longitude}"); } } catch (Exception) { Device.BeginInvokeOnMainThread(() => { var errormessage = new LocationErrorMessage(); MessagingCenter.Send(errormessage, "LocationError"); }); } } }, token); }
public async Task Run(CancellationToken token) { await Task.Run(async() => { System.Diagnostics.Debug.WriteLine(getRunningStateLocationService()); while (getRunningStateLocationService()) { token.ThrowIfCancellationRequested(); try { await Task.Delay(2000); var request = new GeolocationRequest(GeolocationAccuracy.High); var location = await Geolocation.GetLocationAsync(request); if (location != null) { var message = new LocationMessage { Latitude = location.Latitude, Longitude = location.Longitude }; Device.BeginInvokeOnMainThread(() => { MessagingCenter.Send <LocationMessage>(message, "Location"); }); } } catch (Exception ex) { Device.BeginInvokeOnMainThread(() => { var errormessage = new LocationErrorMessage(); MessagingCenter.Send <LocationErrorMessage>(errormessage, "LocationError"); }); } } return; }, token); }
public async Task Run(CancellationToken token) { await Task.Run(async() => { while (!stopping) { token.ThrowIfCancellationRequested(); try { await Task.Delay(2000); var request = new GeolocationRequest(GeolocationAccuracy.High); var location = await Geolocation.GetLocationAsync(request); if (location != null) { var message = new LocationMessage { Latitude = location.Latitude, Longitude = location.Longitude }; Device.BeginInvokeOnMainThread(() => { MessagingCenter.Send <LocationMessage>(message, "Location"); }); } } catch (Exception ex) { Device.BeginInvokeOnMainThread(() => { var errormessage = new LocationErrorMessage(); MessagingCenter.Send <LocationErrorMessage>(errormessage, "LocationError"); }); } } }, token); }