/// <summary> /// 设定天气 /// </summary> /// <param name="mWeather"></param> private void SetWeather(WeatherStructure mWeather) {//天气图标 BitmapSource mSource = null; //根据天气情况匹配对应的天气图标 switch (mWeather.weather) { case "晴": mSource = new BitmapImage(new Uri("Resources\\Weather\\Weather_1.png", UriKind.Relative)); break; case "晴转多云": mSource = new BitmapImage(new Uri("Resources\\Weather\\Weather_2.png", UriKind.Relative)); break; case "多云转晴": mSource = new BitmapImage(new Uri("Resources\\Weather\\Weather_3.png", UriKind.Relative)); break; case "雷阵雨": mSource = new BitmapImage(new Uri("Resources\\Weather\\Weather_5.png", UriKind.Relative)); break; case "小到中雨": mSource = new BitmapImage(new Uri("Resources\\Weather\\Weather_4.png", UriKind.Relative)); break; case "小到中雪": mSource = new BitmapImage(new Uri("Resources\\Weather\\Weather_7.png", UriKind.Relative)); break; } //设定温度 Weather_Tem.SetValue(Label.ContentProperty, mWeather.temp1); //设定城市 Weather_City.SetValue(Label.ContentProperty, mWeather.city); //设定描述 Weather_Descr.SetValue(Label.ContentProperty, mWeather.weather); //设定图标 Weather_Icon.SetValue(System.Windows.Controls.Image.SourceProperty, mSource); }
/// <summary> /// 初始化天气 /// </summary> private void InitWeather() { //如果网络已连接,则获取最新的天气,否则读取本地天气 if (WebState.isConneted()) { //获取天气 WeatherStructure mWeather = WebWeather.getWeatherByCode(WebWeather.getCodeByIP()); SetWeather(mWeather); //更新本地数据 File.Delete(Config + "\\Weather.xml"); XmlDB mDB = XmlDBFactory.CreatXmlDB("Weather", Config + "\\Weather.xml", 1); mDB.Insert(mWeather); mDB.Commit(); } else { XmlDB mDB = XmlDBFactory.LoadXmlDB(Config + "\\Weather.xml"); List <object> mObjects = mDB.ReadByObject(new WeatherStructure()); WeatherStructure mWeather = (WeatherStructure)mObjects[0]; SetWeather(mWeather); } }