Пример #1
0
        public async void OnGetWeatherClicked(object sender, EventArgs args)
        {
            string location = txtLocation.Text.Trim();

            try
            {
                var wr = await owms.GetWeather(location);

                if (wr != null)
                {
                    lblTemp.Text     = String.Format("Temperature: {0}°F", (int)wr.MainWeather.Temp);
                    lblTempBig.Text  = String.Format("{0}°", (int)wr.MainWeather.Temp);
                    lblHighTemp.Text = String.Format("High: {0}°F", (int)wr.MainWeather.MaximumTemp);
                    lblLowTemp.Text  = String.Format("Low: {0}°F", (int)wr.MainWeather.MinimumTemp);

                    // TEXT-TO-SPEECH INTEGRATION
                    string greeter = "";
#if WINDOWS_PHONE
                    greeter = "Hi, I'm Cortana";
#elif __IOS__
                    greeter = "My name is Siri";
#elif __ANDROID__
                    greeter = "This is the voice of Google";
#endif
                    string weatherMessageTemplate = "{0}. The current temperature in {1} is {2}°F, with a high today of {3}° and a low of {4}°.";
                    string weatherMessage         = string.Format(weatherMessageTemplate, greeter, wr.Name, (int)wr.MainWeather.Temp, (int)wr.MainWeather.MaximumTemp, (int)wr.MainWeather.MinimumTemp);
                    DependencyService.Get <ITextToSpeech>().Speak(weatherMessage);
                }
            }
            catch (Exception ex)
            {
                //TO DO: Log the exception somewhere
                DisplayAlert("Error", "Unable to retrieve weather data. Please verify the city name and your Internet connection and try again.", "Ok", "");
            }
        }
Пример #2
0
        public async void OnGetWeatherClicked(object sender, EventArgs args)
        {
            // Get the location from the user (minus surrounding whitespace)
            string location = txtLocation.Text.Trim();

            // Make sure the location is not empty and display a message to the user
            if (location.Length == 0)
            {
                await DisplayAlert("Missing Location", "Please enter a valid location (e.g. city, state) and try again.", "Ok");

                return;
            }

            try
            {
                // Call the eather service and await the call
                var wr = await owms.GetWeather(location);

                if (wr != null)
                {
                    // Populate the labels in the form with the weather data
                    lblTemp.Text     = $"Temperature: {(int) wr.main.temp}°F";
                    lblTempBig.Text  = $"{(int) wr.main.temp}°";
                    lblHighTemp.Text = $"High: {(int) wr.main.temp_max}°F";
                    lblLowTemp.Text  = $"Low: {(int) wr.main.temp_min}°F";

                    // TEXT-TO-SPEECH INTEGRATION
                    string greeter = "";
                    // Customize the speech intro depending on the platform
#if WINDOWS_PHONE || WINDOWS_UWP
                    greeter = "Hi, I'm Cortana";
#elif __IOS__
                    greeter = "My name is Siri";
#elif __ANDROID__
                    greeter = "This is the voice of Google";
#endif
                    // Build a message string to be spoken out loud
                    string weatherMessageTemplate =
                        "{0}. The current temperature in {1} is {2}°F, with a high today of {3}° and a low of {4}°.";
                    string weatherMessage = string.Format(weatherMessageTemplate, greeter, wr.name, (int)wr.main.temp,
                                                          (int)wr.main.temp_max, (int)wr.main.temp_min);
                    // Call the Text-to-Speech Dependency service on each platform to play the weather message with
                    // the platform's speech synthesizer
                    DependencyService.Get <ITextToSpeech>().Speak(weatherMessage);

                    DependencyService.Get <IMetricsManagerService>().TrackEvent("GetWeather");
                }
                else
                {
                    DependencyService.Get <IMetricsManagerService>().TrackEvent("NullWeather");
                }
            }
            catch (Exception ex)
            {
                //TO DO: Send specific exception data to HockeyApp
                DependencyService.Get <IMetricsManagerService>().TrackEvent("GetWeatherError");
                await DisplayAlert("Error", "Unable to retrieve weather data. Please verify the city name and your Internet connection and try again.", "Ok");
            }
        }