分享到...
复制网址 邮件 QQ空间 新浪微博 腾讯微博 微信 人人网 易信 网易微博 搜狐微博 QQ好友 开心网 飞信 豆瓣 一键分享 查看更多(122)

这是什么工具?
JiaThis

天气预报源代码

先上效果图

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

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

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


作者QQ:29992379

技术交流群:231451121

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

点击Q我

  

分类:  [2]WindowsPhone7
标签:  wp7,  源代码,  天气预报
绿色通道:  好文要顶  关注我  收藏该文 与我联系 

韩弈风

关注 - 3

粉丝 - 77

+加关注

12
0
(请您对文章做出评价)

« 上一篇: 手机归属地查询源码

» 下一篇: 记事本源代码

posted @  2012-03-21 20:57  韩弈风 阅读( 3430) 评论( 22)  编辑  收藏


#1楼 2012-03-21 21:09  artwl 

不错啊
支持(0) 反对(0)

#2楼 2012-03-21 21:37  Wot.Guo 

另下载地址使用115下载不太方便,地址被分为上下行,不方便点击下载!既然通过审核,应该给出微软的地址!或者给出二维码图片!如果可以的话,求源码wot.guo@gmail.com!
支持(0) 反对(0)

#3楼 2012-03-21 22:46  误入IT派 

先赞一个 楼主。 后天 、大后天Icon ; 汗惨了。
支持(0) 反对(0)

#4楼 2012-03-22 00:36  GavinDream 

mark,一样楼主开源
支持(0) 反对(0)

#5楼 2012-03-22 00:36  GavinDream 

mark,希望楼主开源
支持(0) 反对(0)

#6楼 [ 楼主2012-03-22 09:24  韩弈风 

@误入IT派

怎么惨了啊
支持(0) 反对(0)

#7楼 [ 楼主2012-03-22 09:29  韩弈风 

@Wot.Guo

http://www.windowsphone.com/zh-CN/apps/336b0bd8-7114-4c92-956b-a0099ef37503

虽然功过了微软的审核,我不大喜欢微软的marketplace,115网盘里面的的可以给越狱和非越狱的机子部署。
支持(0) 反对(0)

#8楼 [ 楼主2012-03-22 09:30  韩弈风 

@Wot.Guo

115网盘里面的是最新的。
支持(0) 反对(0)

#9楼 2012-03-22 09:42  syx278250658 

1、谷歌数据准确嘛?

2、谷歌数据全嘛?比如像UC天气那样的晾晒等指数
支持(0) 反对(0)

#10楼 [ 楼主2012-03-22 10:01  韩弈风 

@syx278250658

没办法,免费啊,找了很多,就谷歌一家免费。我是个学生,没有什么公司给我提供api接口。
支持(0) 反对(0)

#11楼 2012-03-22 10:10  xhma44 

把源代码发来研究下嘛,44574736@qq.com
支持(0) 反对(0)

#12楼 2012-03-22 13:09  丿小瓶盖 

LZ,有更详细的吗?研究研究呗
支持(0) 反对(0)

#13楼 2012-03-23 08:43  一晴 

界面做得很不错。
支持(0) 反对(0)

#14楼 2012-03-23 21:38  Arthars 

UI做的很好~~~NB~
支持(0) 反对(0)

#15楼 2012-03-25 14:49  林林枫枫 

en 不错,顶一下
支持(0) 反对(0)

#16楼 2012-04-05 09:17  kaituozhe345 

楼主是海天学院的????
支持(0) 反对(0)

#17楼 [ 楼主2012-04-05 09:19  韩弈风 

@kaituozhe345

你根据什么
支持(0) 反对(0)

#18楼 2012-04-05 10:38  kaituozhe345 

根据你发布软件用的开发者名字啊呵呵
支持(0) 反对(0)

#19楼 [ 楼主2012-04-05 10:58  韩弈风 

@kaituozhe345

呵呵 可有人说海天军团抄袭我的应用哦,你没这么想吗。
支持(0) 反对(0)

#20楼 2012-04-05 11:00  kaituozhe345 

这么说到提醒我了呵呵,有也是我是略懂略懂
支持(0) 反对(0)

#21楼 2012-04-10 16:12  _雪松_ 

确实很有才。
支持(0) 反对(0)

#22楼 2012-05-31 17:58  MessageDream 

楼主是海天的吗?
支持(0) 反对(0)

刷新评论 刷新页面 返回顶部
最新IT新闻:

·  比特币基金会选举争议人物任董事 引发内部动荡

·  Beats遭公司前高管起诉 索赔超2000万美元

·  谷歌收购视觉信息翻译应用Word Lens

·  苹果谷歌专利纠纷和解 双方同意放弃所有诉讼

·  聚美优品创始人陈欧身价达15.75亿美元

»  更多新闻...
最新知识库文章:

·  单元测试本质:面向逻辑块

·  Windows平台网站图片服务器架构的演进

·  软中断Software Interrupt

·  大型网站架构演变和知识体系

·  我的成长磨练:每天写博客

»  更多知识库文章...

网站统计

windows phone7天气预报源代码相关推荐

  1. 郑州5月份的windows phone7小聚

    今天把5月份在郑州大学办的windows phone7活动照片贴一下,拍的效果不是太好,请大家见谅. 筹备,贴条- 由于是windows phone7主题,我先做了一些windows phone7开发 ...

  2. 一起学windows phone7开发(二十二.使用系统资源)

    在windows phone7中,系统预先设置了一些资源,这样可以保证风格的一致性,在April版本时,这些资源是在创建工程时写在了App.xaml中,但到了beta版后就被隐藏了. 一.系统资源: ...

  3. 操作系统源代码_国产操作系统“之光”?Windows XP绝密源代码泄露,BT种子已在网上疯传...

    微软的Windows操作系统是目前使用人数最多.覆盖最广的桌面操作系统,从安全角度来看,其系统源代码对于公众而言可以说是绝密.不过......现在......,黑客在4Chan平台上以BT种子文件的形 ...

  4. Windows phone7 开发-Zune software is not launched 【转】

    刚学习Windows Phone7开发,遇到这个配置错误,在此记录一下. 错误提示:Zune软件未安装 Zune software is not installed. Install the late ...

  5. Windows phone7 软件发布:理财计算器(包括wp7房贷计算器,wp7个税计算器,wp7存款利息计算器)...

    前一段时间,需要经常用到贷款计算器的功能,这样有利于我们做出更好的决策.但是我们只能通过银行的工作人员的计算器来计算,给我带来了极大的不便和损失,由此便萌生了开发一个Windows phone7版的贷 ...

  6. 【飞秋】一起学Windows Phone7开发(十三.二 按钮控件)

    在Silverlight中有多种按钮控件,这些控件在Windows phone7中也都得到了很好的支持. 一.Button: 这个控件只是一个基础控件,通过blend可以创建出多种效果的按钮来. &l ...

  7. 一起学习Windows Phone7开发(十八. Windows Phone7 Toolkit)

    之前写过Silverlight toolkit 在windows phone7中的应用,那个应该算是山寨版了吧,因为毕竟不是为windows phone7专门开发的,在使用上兼容性上都有问题,现在就不 ...

  8. Windows phone7 动态添加控件

    Windows phone7 动态添加控件 2011-03-29 3:48 using System; using System; using System.Collections.Generic; ...

  9. 我为什么看好Windows Phone7与XBOX Live的互联

    有iPhone4和形形色色的Android手机摆在前头,windows phone7前路多少显得有些凶险.但phone7会以与xbox360的游戏互联性作为突破口,挖掘我们并不熟悉的一块市场.另外,有 ...

最新文章

  1. 如何在Swift中串联或合并数组?
  2. 解决Minikube start卡住的方法
  3. NIPS 2016 | Best Paper, Dual Learning, Review Network, VQA 等论文选介
  4. windows的python切换环境_Windows下的python虚拟环境设置
  5. Flask 中的蓝图 Blueprint
  6. 内核运行之前访问IO
  7. python之struct详解_Python struct模块解析
  8. 2019xman-shellmaster wp
  9. C#生成不重复随机数(随机宝箱)
  10. .net使用Microsoft.Office给word添加自定义水印
  11. 贪婪洞窟2 素材解密
  12. Kafka使用报错Subscription to topics, partitions and pattern are mutually exclusive
  13. 华强北山寨“AirPods 3”出圈,油管博主直夸好
  14. The Devil Wears Prada-3
  15. 如何搭建一个独立博客——简明Github Pages与Hexo教程
  16. 最推荐雇主No.1,美版“支付宝”实力碾压FANNG
  17. Bootstrap从入门到实战---巨幕和旋转图标
  18. 纯正弦波逆变器boost和逆变
  19. MongoDB面试题问题以及参考答案
  20. 自己动手丰衣足食——更换氙气灯

热门文章

  1. 【C++】数据类型|数据类型转换|数据表达处理
  2. cas服务器作用,CAS服务器搭建
  3. 《游戏系统设计十》从零复刻王者荣耀活动系统,策划都能看得懂的活动系统,源码奉送
  4. Python学习(一)——海龟绘图
  5. vasp 模拟退火_物理学报
  6. 基于selenium框架对当当网图书相关数据爬取
  7. 法国推出新型卫星地图Geoportail!欲挑战Google
  8. 如何保证战略落地_如何做好战略分解,确保战略执行落地?
  9. springboot大学生时间管理分析系统毕业设计源码130930
  10. ubuntu 安装flash插件