天气预报源代码
先上效果图
在这里更正一下,黑天的天气预报界面截图的时候,当时操作系统的时间是错误的,导致模拟器里显示2008年。
天气预报是我学WindowsPhone7以来的第一个作品。用的是谷歌的api。功能一个下午就写完了,还是这个界面的设计加P图费了我不少的时间。这个应用已经通过了微软的审核。
下载地址:
http://115.com/file/e7b668w9#
Weather.xap
有什么问题请联系QQ:29992379
由于界面过于复杂,为了实现高效的重用性,我使用了用户自定义控件。通过后台对象的实例化生成的界面。没有办法把所有的代码都展现出来。
1: // 应用程序启动(例如,从“开始”菜单启动)时执行的代码
2: // 此代码在重新激活应用程序时不执行
3: private void Application_Launching(object sender, LaunchingEventArgs e)
4: {
5: if (!IsolatedStorageSettings.ApplicationSettings.Contains("City"))
6: {
7: List<CityWeatherInfo> citys = new List<CityWeatherInfo>();
8: IsolatedStorageSettings.ApplicationSettings["City"] = citys;
9: IsolatedStorageSettings.ApplicationSettings.Save();
10: }
11: }
1: List<CityWeatherInfo> citys = new List<CityWeatherInfo>();
我使用的是IsolatedStorageSettings.ApplicationSettings存储的城市信息。下面为添加城市信息的代码。
1: //添加城市
2: private void AddCityButton_Click(object sender, RoutedEventArgs e)
3: {
4:
5: if ((citys.Where(list => list.CityName == CityNameTextBox.Text).ToList()).Count == 0)
6: {
7: citys.Add(new CityWeatherInfo() { CityGuid = Guid.NewGuid().ToString(), CityName = CityNameTextBox.Text });
8: IsolatedStorageSettings.ApplicationSettings["City"] = citys;
9: IsolatedStorageSettings.ApplicationSettings.Save();
10: }
11: IsolatedStorageSettings.ApplicationSettings["City"] = citys;
12: IsolatedStorageSettings.ApplicationSettings.Save();
13: NavigationService.Navigate(new Uri("/Loading.xaml?cityName=" + CityNameTextBox.Text+"&AndGoPage=MainPage", UriKind.RelativeOrAbsolute));
14: }
1: //删除城市
2: private void linkButtonDel_Click(object sender, RoutedEventArgs e)
3: {
4: for (int i = 0; i < citys.Count; i++)
5: {
6: if (citys[i].CityGuid == ((HyperlinkButton)sender).Tag.ToString())
7: {
8: citys.RemoveAt(i);
9: }
10: }
11: IsolatedStorageSettings.ApplicationSettings["City"] = citys;
12: IsolatedStorageSettings.ApplicationSettings.Save();
13: BindData();
14: }
先上获取天气信息的代码,我自己写了个获取谷歌天气信息的工具类,生成了DLL然后引用进来使用的。
给城市赋天气信息
1: public partial class Loading : PhoneApplicationPage
2: {
3: List<CityWeatherInfo> citys = new List<CityWeatherInfo>();
4: WebClient client = new WebClient();
5: string cityName = "济南";
6: public Loading()
7: {
8: InitializeComponent();
9: citys = IsolatedStorageSettings.ApplicationSettings["City"] as List<CityWeatherInfo>;
10: }
11: private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
12: {
13: LoadingData();
14: }
15: string AndGoPage = "MainPage";
16: public void LoadingData()
17: {
18: AndGoPage = NavigationContext.QueryString["AndGoPage"];
19: if (NavigationContext.QueryString.ContainsKey("cityName"))
20: {
21: cityName = NavigationContext.QueryString["cityName"];
22: }
23: client.OpenReadAsync(new Uri("http://www.google.com/ig/api?hl=zh-cn&oe=utf8&weather=" + cityName, UriKind.RelativeOrAbsolute));
24: client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
25: }
26:
27: void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
28: {
29: XElement xmlWeather;
30: try
31: {
32: xmlWeather = XElement.Load(e.Result);
33: }
34: catch (Exception)
35: {
36: MessageBox.Show("获取城市信息失败");
37: NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
38: return;
39: }
40: foreach (var item in citys)
41: {
42: if (item.CityName == cityName)
43: {
44: item.TodayIcon = GoogleWeatherHelper.GetTodayIcon(xmlWeather);
45: item.LastUpdateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
46: item.Humidity = GoogleWeatherHelper.GetHumidity(xmlWeather);
47: try
48: {
49: item.WindCondition = GoogleWeatherHelper.GetWindCondition(xmlWeather);
50: }
51: catch (Exception)
52: {
53: item.WindCondition = "未知";
54: }
55: item.TodayWeek = GoogleWeatherHelper.GetTodayWeek(xmlWeather);
56: item.TodayIcon = GoogleWeatherHelper.GetTodayIcon(xmlWeather);
57: item.TodayLow = GoogleWeatherHelper.GetTodayLow(xmlWeather);
58: item.TodayHight = GoogleWeatherHelper.GetTodayHight(xmlWeather);
59: item.TodayCondition = GoogleWeatherHelper.GetTodayCondition(xmlWeather);
60:
61: item.TomorrowWeek = GoogleWeatherHelper.GetTomorrowWeek(xmlWeather);
62: item.TomorrowIcon = GoogleWeatherHelper.GetTomorrowIcon(xmlWeather);
63: item.TomorrowLow = GoogleWeatherHelper.GetTomorrowLow(xmlWeather);
64: item.TomorrowHight = GoogleWeatherHelper.GetTomorrowHight(xmlWeather);
65: item.TomorrowCondition = GoogleWeatherHelper.GetTomorrowCondition(xmlWeather);
66:
67: item.HouTianWeek = GoogleWeatherHelper.GetHouTianWeek(xmlWeather);
68: item.HouTianIcon = GoogleWeatherHelper.GetHouTianIcon(xmlWeather);
69: item.HouTianLow = GoogleWeatherHelper.GetHouTianLow(xmlWeather);
70: item.HouTianHight = GoogleWeatherHelper.GetHouTianHight(xmlWeather);
71: item.HouTianCondition = GoogleWeatherHelper.GetHouTianCondition(xmlWeather);
72:
73: item.DaHouTianWeek = GoogleWeatherHelper.GetDaHouTianWeek(xmlWeather);
74: item.DaHouTianIcon = GoogleWeatherHelper.GetDaHouTianIcon(xmlWeather);
75: item.DaHouTianLow = GoogleWeatherHelper.GetDaHouTianLow(xmlWeather);
76: item.DaHouTianHight = GoogleWeatherHelper.GetDaHouTianHight(xmlWeather);
77: item.DaHouTianCondition = GoogleWeatherHelper.GetDaHouTianCondition(xmlWeather);
78: }
79: }
80: IsolatedStorageSettings.ApplicationSettings["City"] = citys;
81: IsolatedStorageSettings.ApplicationSettings.Save();
82: NavigationService.Navigate(new Uri("/" + AndGoPage + ".xaml?cityName=" + cityName, UriKind.RelativeOrAbsolute));
83: }
84: }
首页显示城市列表和城市精简天气信息的实现代码
1: public partial class MainPage : PhoneApplicationPage
2: {
3: List<CityWeatherInfo> citys = new List<CityWeatherInfo>();
4: public MainPage()
5: {
6: InitializeComponent();
7: citys = IsolatedStorageSettings.ApplicationSettings["City"] as List<CityWeatherInfo>;
8: BingUI();
9: }
10:
11: private void BingUI()
12: {
13: string DayOrNight;
14: int hour = DateTime.Now.Hour;
15: ImageBrush img = new ImageBrush();
16: DayOrNight = TimeTools.GetDayorNight();
17: img.ImageSource = new BitmapImage(new Uri("/Images/Back/" + DayOrNight + ".jpg", UriKind.RelativeOrAbsolute));
18:
19: LayoutRoot.Background = img;
20: foreach (var item in citys)
21: {
22: AddCity(item.CityName, item.TodayLow + "℃~" + item.TodayHight + "℃", "/Images/Forecasts/" + DayOrNight + "/" + item.TodayIcon + ".png");
23: }
24: }
25:
26: private void ApplicationBarIconButton_Click(object sender, EventArgs e)
27: {
28: NavigationService.Navigate(new Uri("/CityListEdit.xaml", UriKind.RelativeOrAbsolute));
29: }
30:
31: private void AddCity(string cityName, string cityTemperature, string WeatherIconPath)
32: {
33: CityTileData cityData = new CityTileData();
34: cityData.cityTemperature = cityTemperature;
35: cityData.cityWeatherIcon = WeatherIconPath;
36: CityTile city = new CityTile();
37: city.DataContext = cityData;
38: city.cityName.Content = cityName;
39: city.Width = 184;
40: city.Height = 105;
41: city.Margin = new Thickness(15, 10, 15, 10);
42: wrapPanelCityList.Children.Add(city);
43: city.cityName.Click += new RoutedEventHandler(cityName_Click);
44: }
45:
46: void cityName_Click(object sender, RoutedEventArgs e)
47: {
48: NavigationService.Navigate(new Uri("/Loading.xaml?cityName=" + ((Button)sender).Content + "&AndGoPage=WeatherView", UriKind.RelativeOrAbsolute));
49: }
50:
51: protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
52: {
53: e.Cancel = true;
54: if (MessageBox.Show("", "确定要退出程序吗", MessageBoxButton.OKCancel)==MessageBoxResult.OK)
55: {
56: App.Quit();
57: }
58: base.OnBackKeyPress(e);
59: }
60:
61: private void ApplicationBarIconButton2_Click(object sender, EventArgs e)
62: {
63: NavigationService.Navigate(new Uri("/About.xaml", UriKind.RelativeOrAbsolute));
64: }
65: }
单个城市的详细天气信息以及未来4天的天气情况实现代码。
1: public partial class WeatherView : PhoneApplicationPage
2: {
3: List<CityWeatherInfo> citys = new List<CityWeatherInfo>();
4: public WeatherView()
5: {
6: InitializeComponent();
7: citys = IsolatedStorageSettings.ApplicationSettings["City"] as List<CityWeatherInfo>;
8: }
9: string cityName = "济南";
10: string TodayIcon = "";
11: string TodayInfo = "未知";
12: private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
13: {
14: if (NavigationContext.QueryString.ContainsKey("cityName"))
15: {
16: cityName = NavigationContext.QueryString["cityName"];
17: }
18: BingUI();
19: BingData();
20: }
21: string DayOrNight;
22: private void BingData()
23: {
24: foreach (var item in citys)
25: {
26: if (item.CityName==cityName)
27: {
28: imgWeather.Source = new BitmapImage(new Uri("/Images/Weathericon/" + DayOrNight + "/" + item.TodayIcon + ".png", UriKind.RelativeOrAbsolute));
29:
30: txtTodayTemperature.Text = item.TodayLow + "℃~" + item.TodayHight + "℃";
31: txtCityName.Text = item.CityName;
32: txtLastUpdateTime.Text = item.LastUpdateTime;
33: TodayInfo = item.TodayCondition;
34: txtTodayInfo.Text = "今日天气实况:" + item.TodayCondition + ";气温:" + txtTodayTemperature.Text + ";" + item.WindCondition + ";" + item.Humidity;
35: TodayIcon = item.TodayIcon;
36: forecastTile1.WhichDay = item.TodayWeek;
37: forecastTile1.Weathericon = new BitmapImage(new Uri("/Images/Forecasts/" + DayOrNight + "/" + item.TodayIcon + ".png", UriKind.RelativeOrAbsolute));
38: forecastTile1.Temperature = item.TodayLow + "°/" + item.TodayHight + "°";
39:
40: forecastTile2.WhichDay = item.TomorrowWeek;
41: forecastTile2.Weathericon = new BitmapImage(new Uri("/Images/Forecasts/" + DayOrNight + "/" + item.TomorrowIcon + ".png", UriKind.RelativeOrAbsolute));
42: forecastTile2.Temperature = item.TomorrowLow + "°/" + item.TomorrowHight + "°";
43:
44: forecastTile3.WhichDay = item.HouTianWeek;
45: forecastTile3.Weathericon = new BitmapImage(new Uri("/Images/Forecasts/" + DayOrNight + "/" + item.HouTianIcon + ".png", UriKind.RelativeOrAbsolute));
46: forecastTile3.Temperature = item.HouTianLow + "°/" + item.HouTianHight + "°";
47:
48: forecastTile4.WhichDay = item.DaHouTianWeek;
49: forecastTile4.Weathericon = new BitmapImage(new Uri("/Images/Forecasts/" + DayOrNight + "/" + item.DaHouTianIcon + ".png", UriKind.RelativeOrAbsolute));
50: forecastTile4.Temperature = item.DaHouTianLow + "°/" + item.DaHouTianHight + "°";
51: }
52: }
53: }
54:
55: private void BingUI()
56: {
57: int hour = DateTime.Now.Hour;
58: ImageBrush img = new ImageBrush();
59: DayOrNight = TimeTools.GetDayorNight();
60: img.ImageSource = new BitmapImage(new Uri("/Images/Back/"+DayOrNight+".jpg", UriKind.RelativeOrAbsolute));
61: LayoutRoot.Background = img;
62: }
63:
64: protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
65: {
66: e.Cancel = true;
67: NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
68: base.OnBackKeyPress(e);
69: }
70:
71: private void ApplicationBarIconButton_Click(object sender, EventArgs e)
72: {
73: ChangeTile.ChangeTileByCityName(cityName, TodayIcon, TodayInfo, txtTodayTemperature.Text);
74: }
75: }
今天先更新到这个地方。尽请期待…..