コード例 #1
0
        public async void GetWeatherBtn_Clicked(object sender, EventArgs e)
        {
            try
            {
                aparece();
                if (!String.IsNullOrEmpty(cidadeEntry.Text))
                {
                    Tempo tempo = await DataService.GetWeather(cidadeEntry.Text);

                    if (tempo != null)
                    {
                        listView.SelectedItem     = false;
                        salvarFavorito.IsEnabled  = true;
                        verDetalhes.IsEnabled     = true;
                        removerFavorito.IsEnabled = false;
                        BindingContext            = tempo;
                    }
                    else
                    {
                        DisplayAlert("Alerta", "Cidade não encontrada!", "OK");
                    }
                }
                await Task.Delay(3000);

                some();
            }
            catch (Exception ex)
            {
                some();
                if (ex != null)
                {
                    ;
                }
            }
        }
コード例 #2
0
        public static async Task <Tempo> GetWeather(string nomeDaCidade)
        {
            string key         = "ac7c243e9a839ca5df46142cca279738";
            string queryString = "http://api.openweathermap.org/data/2.5/weather?q="
                                 + nomeDaCidade + "&appid=" + key + "&units=metric&lang=pt";

            dynamic results = await DataService.getDataFromService(queryString).ConfigureAwait(false);

            if (results["weather"] != null)
            {
                Tempo tempo = new Tempo();
                tempo.Titulo       = (string)results["name"];
                tempo.Temperatura  = (string)results["main"]["temp"] + "° C";
                tempo.Vento        = (string)results["wind"]["speed"] + " km/h";
                tempo.Humidade     = (string)results["main"]["humidity"] + " %";
                tempo.Visibilidade = (string)results["weather"][0]["main"];
                tempo.ImageUrl     = "http://openweathermap.org/img/w/" + (string)results["weather"][0]["icon"] + ".png";

                DateTime time    = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
                DateTime sunrise = time.AddSeconds((double)results["sys"]["sunrise"]);
                DateTime sunset  = time.AddSeconds((double)results["sys"]["sunset"]);
                tempo.NascerDoSol = sunrise.ToString() + " UTC";
                tempo.PorDoSol    = sunset.ToString() + " UTC";
                return(tempo);
            }
            else
            {
                return(null);
            }
        }
コード例 #3
0
        private async void removerFavorito_Clicked(object sender, EventArgs e)
        {
            Tempo tempo = BindingContext as Tempo;

            if (tempo.Local != null)
            {
                await App.DataBase.DeleteItemAsync(tempo.Local);

                listView.ItemsSource = await App.DataBase.GetItemsAsync();

                removerFavorito.IsEnabled = false;
            }
        }
コード例 #4
0
        public async void OnListItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            aparece();
            var local = e.SelectedItem as Local;

            if (local != null)
            {
                Tempo tempo = await DataService.GetWeather(local.Nome);

                if (tempo != null)
                {
                    salvarFavorito.IsEnabled = true;
                    verDetalhes.IsEnabled    = true;
                    BindingContext           = tempo;
                    tempo.Local = local;
                    removerFavorito.IsEnabled = true;
                }
            }
            await Task.Delay(3000);

            some();
        }