void weatherClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { if (e.Error == null) { Task.Factory.StartNew(() => { DataContractJsonSerializer dc = new DataContractJsonSerializer(typeof(WorldWeatherInfo)); WorldWeatherInfo worldWeatherInfo = (WorldWeatherInfo)dc.ReadObject(e.Result); ObservableCollection <CityWeather> tempWeatherInCities = new ObservableCollection <CityWeather>(); foreach (CityWeatherInfo weatherInfo in worldWeatherInfo.list) { CityWeather cityWeather = ViewModelSource.Create(() => new CityWeather(weatherInfo)); string cityWithId = string.Format("{0};{1}", cityWeather.City, cityWeather.CityID); if (capitals.Contains(cityWeather.City) || capitals.Contains(cityWithId)) { if (cityWeather.WeatherDescriptions != null && cityWeather.WeatherDescriptions.Count > 0) { cityWeather.WeatherIconPath = "http://openweathermap.org/img/w/" + cityWeather.WeatherDescriptions[0].IconName + ".png"; } tempWeatherInCities.Add(cityWeather); } } lock (weatherLocker) { WeatherInCities = tempWeatherInCities; } this.uiDispatcher.Invoke(new Action(() => UpdateCurrentTemperatureType())); }); } }
void weatherClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { WebClient weatherClient = sender as WebClient; weatherClient.OpenReadCompleted -= weatherClient_OpenReadCompleted; if (e.Cancelled || e.Error != null) { return; } Stream stream = e.Result; Task.Factory.StartNew(() => { DataContractJsonSerializer dc = new DataContractJsonSerializer(typeof(WorldWeatherInfo)); WorldWeatherInfo worldWeatherInfo = (WorldWeatherInfo)dc.ReadObject(stream); List <CityWeather> citiesWeather = new List <CityWeather>(); foreach (CityCurrentWeatherInfo weatherInfo in worldWeatherInfo.list) { CityWeather cityWeather = new CityWeather(weatherInfo); if (cityWeather.City == "Los Angeles") { LosAngelesWeather = cityWeather; } if (cityWeather.WeatherDescriptions != null && cityWeather.WeatherDescriptions.Count > 0) { cityWeather.WeatherIconPath = OpenWeatherIconPathPrefix + cityWeather.WeatherDescriptions[0].IconName + ".png"; } string cityWithId = string.Format("{0};{1}", cityWeather.City, cityWeather.CityID); if (capitalNames.Contains(cityWeather.City) || capitalNames.Contains(cityWithId)) { citiesWeather.Add(cityWeather); } } weatherInCities = citiesWeather; RaiseReadComplete(); }); }