windows phone7天气预报源代码

天气预报源代码

先上效果图

145236789

在这里更正一下,黑天的天气预报界面截图的时候,当时操作系统的时间是错误的,导致模拟器里显示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然后引用进来使用的。

image

给城市赋天气信息

   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:      }

 

今天先更新到这个地方。尽请期待…..

为了应对人类无休止的贪婪,才要发动一次又一次的技术革命。


作者QQ:29992379

技术交流群:231451121

我将会在我的博客里传上一系列我写的作品以及源代码。希望大家关注。

点击Q我

  

分类:  [2]WindowsPhone7
标签:  wp7源代码天气预报
12
0
(请您对文章做出评价)
« 上一篇: 手机归属地查询源码
» 下一篇: 记事本源代码
posted @  2012-03-21 20:57  韩弈风 阅读( 3430) 评论( 22编辑  收藏

  
#1楼 2012-03-21 21:09  artwl   
不错啊
  
#2楼 2012-03-21 21:37  Wot.Guo   
另下载地址使用115下载不太方便,地址被分为上下行,不方便点击下载!既然通过审核,应该给出微软的地址!或者给出二维码图片!如果可以的话,求源码wot.guo@gmail.com!
  
#3楼 2012-03-21 22:46  误入IT派   
先赞一个 楼主。 后天 、大后天Icon ; 汗惨了。
  
#4楼 2012-03-22 00:36  GavinDream   
mark,一样楼主开源
  
#5楼 2012-03-22 00:36  GavinDream   
mark,希望楼主开源
  
#6楼 [ 楼主2012-03-22 09:24  韩弈风   
@误入IT派
怎么惨了啊
  
#7楼 [ 楼主2012-03-22 09:29  韩弈风   
@Wot.Guo
http://www.windowsphone.com/zh-CN/apps/336b0bd8-7114-4c92-956b-a0099ef37503
虽然功过了微软的审核,我不大喜欢微软的marketplace,115网盘里面的的可以给越狱和非越狱的机子部署。
  
#8楼 [ 楼主2012-03-22 09:30  韩弈风   
@Wot.Guo
115网盘里面的是最新的。
  
#9楼 2012-03-22 09:42  syx278250658   
1、谷歌数据准确嘛?
2、谷歌数据全嘛?比如像UC天气那样的晾晒等指数
  
#10楼 [ 楼主2012-03-22 10:01  韩弈风   
@syx278250658
没办法,免费啊,找了很多,就谷歌一家免费。我是个学生,没有什么公司给我提供api接口。
  
#11楼 2012-03-22 10:10  xhma44   
把源代码发来研究下嘛,44574736@qq.com
  
#12楼 2012-03-22 13:09  丿小瓶盖   
LZ,有更详细的吗?研究研究呗
  
#13楼 2012-03-23 08:43  一晴   
界面做得很不错。
  
#14楼 2012-03-23 21:38  Arthars   
UI做的很好~~~NB~
  
#15楼 2012-03-25 14:49  林林枫枫   
en 不错,顶一下
  
#16楼 2012-04-05 09:17  kaituozhe345   
楼主是海天学院的????
  
#17楼 [ 楼主2012-04-05 09:19  韩弈风   
@kaituozhe345
你根据什么
  
#18楼 2012-04-05 10:38  kaituozhe345   
根据你发布软件用的开发者名字啊呵呵
  
#19楼 [ 楼主2012-04-05 10:58  韩弈风   
@kaituozhe345
呵呵 可有人说海天军团抄袭我的应用哦,你没这么想吗。
  
#20楼 2012-04-05 11:00  kaituozhe345   
这么说到提醒我了呵呵,有也是我是略懂略懂
  
#21楼 2012-04-10 16:12  _雪松_   
确实很有才。
  
#22楼 2012-05-31 17:58  MessageDream   
楼主是海天的吗?

网站统计
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值