コード例 #1
0
        /// <summary>
        /// Get current weather by url from service 
        /// Donwload string JSON 
        /// Deserialize string to WeatherJson.cs class
        /// </summary>
        public Weather(string city)
        {
            City = city;
            Url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%27" + city + "%27)&diagnostics=true&format=json";

            if (CheckInternetConnection().Equals(true))
            {
                using (WebClient wc = new WebClient())
                {
                    result = wc.DownloadString(Url);
                }
                dataObject = JObject.Parse(result);
                try
                {
                    jsonWeather = JsonConvert.DeserializeObject<WeatherJson>(dataObject["query"]["results"]["channel"]["item"]["condition"].ToString());
                }
                catch (System.InvalidOperationException) { Error = "The city is not found"; ErrorCode = 2; return; }          
            }
            else
            {
                Error = "Check internet connection!";
                ErrorCode = 1;
                return;
            }
        }
コード例 #2
0
 /// <summary>
 /// Get forecast weather 
 /// </summary>
 /// <param name="city"></param>
 /// <param name="forecast"></param>
 public Weather(string city, bool forecast)
 {
     _listWeatherForecast = new List<WeatherJson>();
     if (forecast)
     {
         City = city;
         Url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" + city + "%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
         if (CheckInternetConnection().Equals(true))
         {
             using (WebClient wc = new WebClient())
             {
                 result = wc.DownloadString(Url);
             }
             dataObject = JObject.Parse(result);
             try
             {
                 dynamic jsArray = (JArray)JsonConvert.DeserializeObject(dataObject["query"]["results"]["channel"]["item"]["forecast"].ToString());
                 foreach (JObject item in jsArray)
                 {
                     jsonForecast = JsonConvert.DeserializeObject<WeatherJson>(item.ToString());
                     _listWeatherForecast.Add(new WeatherJson()
                     {
                         Day = jsonForecast.Day,
                         Image = GetImageWeather(jsonForecast),
                         Code = jsonForecast.Code,
                         Date = jsonForecast.Date,
                         TempLow = Math.Round((jsonForecast.TempLow - 32) / 1.8, 2),
                         TempHigh = Math.Round((jsonForecast.TempHigh - 32) / 1.8, 2),
                         TempAverage = "Average temp: " + Math.Round((((jsonForecast.TempHigh + jsonForecast.TempLow) / 2) - 32) / 1.8, 2),
                         Conditions =  jsonForecast.Conditions, // Conditions: 
                         City = this.City
                     });
                 }
             }
             catch (System.InvalidOperationException) { Error = "The city is not found"; ErrorCode = 2; return; }         
         }
         else
         {
             Error = "Check internet connection!";
             ErrorCode = 1;
             return;
         }
     }
 }
コード例 #3
0
 /// <summary>
 /// Returns the image depending on weather conditions
 /// </summary>
 /// <param name="_weather"></param>
 /// <returns></returns>
 private BitmapImage GetImageWeather(WeatherJson _weather)
 {
     switch (_weather.Conditions)
     {
         case "Clear":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Sunny.png"));      
         case "Light Rain":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/light_rain.png"));              
         case "Haze":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Haze.png"));
         case "Cloudy":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Cloudy.png"));              
         case "Snow":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Snow.png"));                
         case "Snow Flurries":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Snow.png"));                   
         case "Showers":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Drizzle.png"));
         case "AM Showers":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Drizzle.png")); 
         case "Drizzle":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Drizzle.png"));                   
         case "Freezing Drizzle":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Drizzle.png"));
         case "Thunderstorms":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Thunderstorms.png"));
         case "AM Thundershowers":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Thunderstorms.png"));
         case "Foggy":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Cloudy.png"));                  
         case "Mostly Cloudy":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Mostly Cloudy.png"));               
         case "Partly Cloudy":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Mostly Cloudy.png"));               
         case "Sunny":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Sunny.png"));
         case "Mostly Sunny":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Sunny.png"));
         case "Mostly Clear":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Cloudy.png"));    
         case "Fair":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Sunny.png"));                   
         case "Hot":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/Sunny.png"));                   
         case "Mixed rain and snow":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/rain_snow.png"));                 
         case "Windy":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/wind.png"));                    
         case "Blustery":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/wind.png"));
         case "Mixed rain and sleet":
             return new BitmapImage(new Uri("pack://application:,,,/Images/Weather/wind.png"));
         case "Not available":
             return null; 
         default:
             return null;      
     }
 }