wp7天气预报源代码(二获取谷歌天气数据)公布源代码下载地址
在上一篇文章里已经介绍了这个应用
文章源地址:http://www.cnblogs.com/wildfeng/archive/2012/03/21/2410504.html
由于代码过多,和繁杂的前台页面效果代码,没办法在博文中说明白,还有很多网友要求我公布源代码项目。在文章的最下面我提供了源代码的下载地址。
作者QQ:29992379
这个天气预报用的是谷歌的API,我特意写了个工具类用来解析谷歌天气数据,本文中主要介绍这个工具类。
代码如下:
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来实现了一些效果。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?