private WeatherDetail Search(PlaceModel province, PlaceModel city, PlaceModel district) { WeatherDetail detail = new WeatherDetail(province, city, district); detail.HandleWeather(); return(detail); }
private void buttonSearch_Click(object sender, EventArgs e) { PlaceModel province = null, city = null, district = null; this.InvokeToForm(() => { province = this.comboBoxProvince.ComboBox.SelectedItem as PlaceModel; city = this.comboBoxCity.ComboBox.SelectedItem as PlaceModel; district = this.comboBoxDistrict.ComboBox.SelectedItem as PlaceModel; }); if (province != null && city != null && district != null) { this.InvokeToForm(() => lblStatus.Text = "查询中"); Task.Factory.StartNew(() => { WeatherDetail detail = this.Search(province, city, district); this.Invoke(new Action(() => { this.SetWeather(detail); lblStatus.Text = "已完成"; })); }).ContinueWith(t => { if (t.IsFaulted) { this.InvokeToForm(() => lblStatus.Text = "查询错误,请确保联网正确"); } }); } }
private void SetWeather(WeatherDetail detail) { WeatherDay[] weatherDays = new WeatherDay[] { weatherDay1, weatherDay2, weatherDay3, weatherDay4, weatherDay5, weatherDay6, weatherDay7 }; for (int i = 0; i < weatherDays.Length; i++) { weatherDays[i].Day = detail.Day_1To7[i] ?? ""; weatherDays[i].Info = detail.Info_1To7[i] ?? ""; weatherDays[i].Temperature = detail.Temperature_1To7[i] ?? ""; weatherDays[i].Wind = detail.Wind_1To7[i] ?? ""; weatherDays[i].WeatherStatus = detail.WeatherStatus_1To7[i]; } WeatherDayMore[] weatherDaysMore = new WeatherDayMore[] { weatherDayMore1, weatherDayMore2, weatherDayMore3, weatherDayMore4, weatherDayMore5, weatherDayMore6, weatherDayMore7, weatherDayMore8 }; for (int i = 0; i < weatherDaysMore.Length; i++) { weatherDaysMore[i].Day = detail.Day_7To15[i] ?? ""; weatherDaysMore[i].Info = detail.Info_7To15[i] ?? ""; weatherDaysMore[i].Temperature = detail.Temperature_7To15[i] ?? ""; weatherDaysMore[i].Wind1 = detail.Wind1_7To15[i] ?? ""; weatherDaysMore[i].Wind2 = detail.Wind2_7To15[i] ?? ""; weatherDaysMore[i].WeatherStatus = detail.WeatherStatus_7To15[i]; } }