wp7天气预报源代码(二获取谷歌天气数据)公布源代码下载地址

在上一篇文章里已经介绍了这个应用

文章源地址:http://www.cnblogs.com/wildfeng/archive/2012/03/21/2410504.html

145236789

由于代码过多,和繁杂的前台页面效果代码,没办法在博文中说明白,还有很多网友要求我公布源代码项目。在文章的最下面我提供了源代码的下载地址。

作者QQ:29992379

这个天气预报用的是谷歌的API,我特意写了个工具类用来解析谷歌天气数据,本文中主要介绍这个工具类。

image

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Linq;
using System.Xml.Linq;
namespace GoogleWeather
{
    public static class GoogleWeatherHelper
    {
 
        /// <summary>
        /// 获取城市以及省
        /// </summary>
        /// <param name="xmlWeather">xml数据</param>
        /// <returns></returns>
        public static string GetCity(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_information").First();
            return forecast_information.Element("city").Attribute("data").Value;
        }
        /// <summary>
        /// 获取中文城市名称
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetPostalCode(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_information").First();
            return forecast_information.Element("postal_code").Attribute("data").Value;
        }
        /// <summary>
        /// 获取预报的日期
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetForecastDate(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_information").First();
            return forecast_information.Element("forecast_date").Attribute("data").Value;
        }
        /// <summary>
        /// 获取湿度
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetHumidity(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("current_conditions").First();
            return forecast_information.Element("humidity").Attribute("data").Value;
        }
        /// <summary>
        /// 获取风向
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetWindCondition(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("current_conditions").First();
            return forecast_information.Element("wind_condition").Attribute("data").Value;
        }
        /// <summary>
        /// 获取今天星期
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetTodayWeek(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").First();
            return forecast_information.Element("day_of_week").Attribute("data").Value;
        }
        /// <summary>
        /// 获取今天最低温度
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetTodayLow(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").First();
            return forecast_information.Element("low").Attribute("data").Value;
        }
        /// <summary>
        /// 获取今天最高温度
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetTodayHight(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").First();
            return forecast_information.Element("high").Attribute("data").Value;
        }
        /// <summary>
        /// 获取今天天气图标
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetTodayIcon(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").First();
            return ExtractFileName(forecast_information.Element("icon").Attribute("data").Value);
        }
        /// <summary>
        /// 获取今天天气情况
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetTodayCondition(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").First();
            return forecast_information.Element("condition").Attribute("data").Value;
        }
        /// <summary>
        /// 获取明天星期
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetTomorrowWeek(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(1);
            return forecast_information.Element("day_of_week").Attribute("data").Value;
        }
        /// <summary>
        /// 获取明天最低温度
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetTomorrowLow(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(1);
            return forecast_information.Element("low").Attribute("data").Value;
        }
        /// <summary>
        /// 获取明天最高温度
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetTomorrowHight(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(1);
            return forecast_information.Element("high").Attribute("data").Value;
        }
        /// <summary>
        /// 获取明天天气图标
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetTomorrowIcon(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(1);
            return ExtractFileName(forecast_information.Element("icon").Attribute("data").Value);
        }
        /// <summary>
        /// 获取明天天气情况
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetTomorrowCondition(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(1);
            return forecast_information.Element("condition").Attribute("data").Value;
        }
        /// <summary>
        /// 获取后天星期
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetHouTianWeek(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(2);
            return forecast_information.Element("day_of_week").Attribute("data").Value;
        }
        /// <summary>
        /// 获取后天最低温度
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetHouTianLow(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(2);
            return forecast_information.Element("low").Attribute("data").Value;
        }
        /// <summary>
        /// 获取后天最高温度
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetHouTianHight(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(2);
            return forecast_information.Element("high").Attribute("data").Value;
        }
        /// <summary>
        /// 获取后天天气图标
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetHouTianIcon(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(2);
            return ExtractFileName(forecast_information.Element("icon").Attribute("data").Value);
        }
        /// <summary>
        /// 获取后天天气情况
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetHouTianCondition(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(2);
            return forecast_information.Element("condition").Attribute("data").Value;
        }
        /// <summary>
        /// 获取大后天星期
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetDaHouTianWeek(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(3);
            return forecast_information.Element("day_of_week").Attribute("data").Value;
        }
        /// <summary>
        /// 获取大后天最低温度
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetDaHouTianLow(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(3);
            return forecast_information.Element("low").Attribute("data").Value;
        }
        /// <summary>
        /// 获取大后天最高温度
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetDaHouTianHight(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(3);
            return forecast_information.Element("high").Attribute("data").Value;
        }
        /// <summary>
        /// 获取大后天天气图标
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetDaHouTianIcon(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(3);
            return ExtractFileName(forecast_information.Element("icon").Attribute("data").Value);
        }
        /// <summary>
        /// 获取大后天天气情况
        /// </summary>
        /// <param name="xmlWeather"></param>
        /// <returns></returns>
        public static string GetDaHouTianCondition(XElement xmlWeather)
        {
            XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(3);
            return forecast_information.Element("condition").Attribute("data").Value;
        }
 
        private static string ExtractFileName(string fullFileName)
        {
            string str = fullFileName.Substring(fullFileName.LastIndexOf('/') + 1);
            return str.Substring(0, str.LastIndexOf('.')).Replace("cn_", "");
        }
    }
}

 

整个天气预报项目源代码的下载地址:http://download.csdn.net/detail/wildfeng04/4168595

在以后的博文里我会详细讲解这个应用UI方面的实现,毕竟这个应用亮点全在UI上面。我个人是这么理解的,因为功能代码不是很难,UI的效果比较炫。我用了Storyboard来实现了一些效果。

posted @   巫鸦  阅读(2744)  评论(14编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示