7,655
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Windows.Media.Imaging;
namespace Weather
{
public partial class MainPage : PhoneApplicationPage
{
WeatherInfo weather = null;
string[] weekMsg = { "星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
// 构造函数
public MainPage()
{
InitializeComponent();
}
private void PageLoad(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wb_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://m.weather.com.cn/data/101020100.html",UriKind.Absolute));
}
//下载完成后的处理事件
void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {
//判断现在是否成功
if(e.Result.Length<=0||e.Error!=null||e.Cancelled){
MessageBox.Show("获取天气信息失败!");
return;
}
//创建解析对象
JObject json = JObject.Parse(e.Result);//解析网络请求的结果
weather = new WeatherInfo()
{
city = (string)json["weatherinfo"]["city"],
cityid = (string)json["weatherinfo"]["cityid"],
date_y = (string)json["weatherinfo"]["date_y"],
wind = (string)json["weatherinfo"]["wind1"],
info = (string)json["weatherinfo"]["info"],
temp1 = (string)json["weatherinfo"]["temp1"],
temp2 = (string)json["weatherinfo"]["temp2"],
temp3 = (string)json["weatherinfo"]["temp3"],
temp4 = (string)json["weatherinfo"]["temp4"],
temp5 = (string)json["weatherinfo"]["temp5"],
temp6 = (string)json["weatherinfo"]["temp6"],
weather1 = (string)json["weatherinfo"]["weather1"],
weather2 = (string)json["weatherinfo"]["weather2"],
weather3 = (string)json["weatherinfo"]["weather3"],
weather4 = (string)json["weatherinfo"]["weather4"],
weather5 = (string)json["weatherinfo"]["weather5"],
weather6 = (string)json["weatherinfo"]["weather6"],
week = (string)json["weatherinfo"]["week"],
};
UpdateUi();
}
//跟新UI信息
void UpdateUi()
{
day1.Temp = weather.weather2;
day2.Temp = weather.weather3;
day3.Temp = weather.weather4;
day4.Temp = weather.weather6;
today_date.Text = weather.date_y;
today_temperature.Text = weather.temp1;
today_week.Text = weather.week;
today_describle.Text = weather.info;
this.cityname.Text = weather.city;
int i;
for (i = 0; i < 7; i++)
{
if (weekMsg[i] == weather.week)
break;
}
day1.Weekday = weekMsg[(i + 1) % 7];
day2.Weekday = weekMsg[(i + 2) % 7];
day3.Weekday = weekMsg[(i + 3) % 7];
day4.Weekday = weekMsg[(i + 4) % 7];
day1.ImageUri = GetImgUri(weather.temp2);
day2.ImageUri = GetImgUri(weather.temp3);
day3.ImageUri = GetImgUri(weather.temp4);
day4.ImageUri = GetImgUri(weather.temp5);
today_image.Source = new BitmapImage(new Uri(GetImgUri(weather.temp1),UriKind.Relative));
}
//返回天气图片的Uri
String GetImgUri(string weather)
{
string uri = "/Weather;component/Images/";
if (weather == "晴")
{
return uri + "sunday.jpg";
}
else if (weather == "阴")
{
return uri + "overcast.jpg";
}
else if (weather == "雷阵雨")
{
return uri + "ThunderShower.jpg";
}
else if (weather.Contains("多云"))
{
return uri + "cloudy.jpg";
}
else if (weather.Contains("雨"))
{
return uri + "Rain.jpg";
}
else
{
return uri + "cloudy.jpg";
}
}
}
}