async Task ExecuteGetWeatherCommand() { if (IsBusy) { return; } IsBusy = true; try { WeatherRoot weatherRoot = null; var units = IsImperial ? Units.Imperial : Units.Metric; if (UseGPS) { var gps = await CrossGeolocator.Current.GetPositionAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(false); weatherRoot = await WeatherService.GetWeather(gps.Latitude, gps.Longitude, units).ConfigureAwait(false); } else { //Get weather by city weatherRoot = await WeatherService.GetWeather(Location.Trim(), units).ConfigureAwait(false); } //Get forecast based on cityId Forecast = await WeatherService.GetForecast(weatherRoot, units).ConfigureAwait(false); var unit = IsImperial ? "F" : "C"; Temperature = $"Temp: {weatherRoot?.MainWeather?.Temperature ?? 0}°{unit}"; Condition = $"{weatherRoot.Name}: {weatherRoot?.Weather?[0]?.Description ?? string.Empty}"; IsBusy = false; await CrossTextToSpeech.Current.Speak(Temperature + " " + Condition).ConfigureAwait(false); } catch (Exception e) { DebugServices.Report(e); Temperature = "Unable to get Weather"; } finally { IsBusy = false; } }
//ICommand getWeather; //public ICommand GetWeatherCommand => // getWeather ?? // (getWeather = new Command(async () => await ExecuteGetWeatherCommand())); public async Task ExecuteGetWeatherCommand() { if (IsBusy) { return; } IsBusy = true; try { WeatherRoot weatherRoot = null; var units = IsImperial ? Units.Imperial : Units.Metric; if (UseGPS) { var gps = await CrossGeolocator.Current.GetPositionAsync(10000); weatherRoot = await WeatherService.GetWeather(gps.Latitude, gps.Longitude, units); } else { //Get weather by city weatherRoot = await WeatherService.GetWeather(Location.Trim(), units); } //Get forecast based on cityId Forecast = await WeatherService.GetForecast(weatherRoot.CityId, units); var unit = IsImperial ? "F" : "C"; var temp = (int)(weatherRoot?.MainWeather?.Temperature ?? 0); Temp = $"{temp}°{unit}"; Condition = $"{weatherRoot?.Weather?[0]?.Description ?? string.Empty}"; //CrossTextToSpeech.Current.Speak(Temp + " " + Condition); } catch { Temp = "Unable to get Weather"; } finally { IsBusy = false; } }