public void GetSavedCityFromXml() { try { if (SavedCityInfoList.Count != 0) { Application.Current.Dispatcher.Invoke(() => { SavedCityInfoList.Clear(); }); } foreach (var city in XmlHelper.ReadFromXml()) { Application.Current.Dispatcher.Invoke(() => { SavedCityInfoList.Add(city); }); } if (SavedCityInfoList.Count > 0) { ListSelectItemCityInfo = SavedCityInfoList[0]; } } catch (Exception ex) { ShowErrorInfoStr("从XML中取出已保存城市过程中发生错误!\n" + ex.Message); } }
public void SearchCityAndSaveCityInfo() { if (!InitComplete) { MessageBox.Show("天气信息正在排队初始化中,现在不能新增城市,请稍后再试!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning); return; } try { if (string.IsNullOrEmpty(InputCityNameStr)) { MessageBox.Show("输入城市名为空!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning); return; } FindCityInfoList.Clear(); foreach (var city in WeatherHelper.GetCityInfos(InputCityNameStr)) { FindCityInfoList.Add(city); } #region 加入保存列表、刷新显示 if (FindCityInfoList.Count == 0) { MessageBox.Show("未查询到该城市信息!\n\n仅能查询国内非港澳台地区!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning); return; } foreach (var savedCity in SavedCityInfoList) { foreach (var findCity in FindCityInfoList) { if (savedCity.CityName == findCity.CityName && savedCity.CityCode == findCity.CityCode) { MessageBox.Show("保存失败!保存过程已被拦截。\n" + savedCity.CityName + "已经存在!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning); return; } } } foreach (var city in FindCityInfoList) { SavedCityInfoList.Add(city); } XmlHelper.WriteToXml(SavedCityInfoList.ToList()); FindCityInfoList.Clear(); //ShowErrorInfoStr("请添加完所有城市后手动刷新天气信息!"); WriteLog("\t添加城市:" + InputCityNameStr); MessageBox.Show(InputCityNameStr + " 添加成功!它将排末尾。\n请添加完所有城市后手动刷新天气信息!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information); InputCityNameStr = string.Empty; #endregion } catch (Exception ex) { ShowErrorInfoStr("添加城市过程中发生一个错误!\n" + ex.Message); } }