Пример #1
0
        /// <summary>
        /// 根据城市代码获取天气状况
        /// </summary>
        /// <param name="mCode">城市代码</param>
        public static WeatherStructure getWeatherByCode(string mCode)
        {
            //获取指定城市天气数据
            string          wUrl      = string.Format("http://www.weather.com.cn/data/cityinfo/{0}.html", mCode);
            HttpWebRequest  mRequest  = (HttpWebRequest)HttpWebRequest.Create(wUrl);
            HttpWebResponse mResponse = (HttpWebResponse)mRequest.GetResponse();

            mRequest.ContentType = "text/html";
            mRequest.Method      = "Get";
            Stream       Streams = mResponse.GetResponseStream();
            StreamReader mReader = new StreamReader(Streams, Encoding.UTF8);
            String       mResult = mReader.ReadToEnd();

            mReader.Dispose();
            Streams.Dispose();
            mResponse.Close();
            //处理数据
            mResult = mResult.Substring(mResult.IndexOf(":") + 1, mResult.Length - mResult.IndexOf(":") - 2);
            //解析json
            DataContractJsonSerializer Serializer = new DataContractJsonSerializer(typeof(WeatherStructure));
            MemoryStream     Stream   = new MemoryStream(Encoding.Unicode.GetBytes(mResult));
            WeatherStructure mWeather = (WeatherStructure)Serializer.ReadObject(Stream);

            return(mWeather);
        }
Пример #2
0
 /// <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);
 }