Windows Phone 8.1 开发实例 天气预报

首先感谢林政老师的博客,给了我很大的指导。

准备工作

我的开发环境: – Visual Studio 2013(With Update 4) – Windows Phone 8.1 – Windows 8.1 我使用的是百度天气的api,所以你需要一个百度天气的ak,戳这里申请。记住你ak的值,像这样拼接uri: ?location=城市&output=xml&ak=你的ak 可以获取一个xml格式的返回数据:

具体思路

Created with Raphal 2.1.0异步获取数据解析xml绑定数据

实现

ForecastPeriod类,用来记录每天的天气和数据的绑定:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.ComponentModel;namespace WeatherForecastDIY{天气预报数据类/// </summary>class ForecastPeriod:INotifyPropertyChanged{wind;maxTemperature;minTemperature;date;dayPictureUrl;temperature;Weather{set{if (value != weather){weather = value;OnpropertyChanged(“Weather”);}}get{return weather;}}public string Wind{set{if (value != wind){wind = value;OnpropertyChanged(“Wind”);}}get{return wind;}}public string MaxTemperature{set{if (value != maxTemperature){maxTemperature = value;OnpropertyChanged(“MaxTemperature”);}}get{analysisTemperature();return maxTemperature;}}public string MinTemperature{set{if (value != minTemperature){minTemperature = value;OnpropertyChanged(“MinTemperature”);}}get{analysisTemperature();return minTemperature;}}public string Date{set{if (value != date){date = value;OnpropertyChanged(“Date”);}}get{return date;}}public string DayPictureUrl{set{if (dayPictureUrl != value){dayPictureUrl = value;OnpropertyChanged(“DayPictureUrl”);}}get{return dayPictureUrl;}}public string Temperature{set{if (value != temperature){temperature = value;OnpropertyChanged(“Temperature”);}}get{return temperature;}}分析温度字符串,得到最高最低温(){if (Temperature == null)return;int p = -1;//波浪号的位置for (int i = 0; i < Temperature.Length; i++)if (Temperature[i] == ‘~’) { p = i; break; }if(p==-1)//没有波浪号{maxTemperature = Temperature;minTemperature = Temperature;}else{maxTemperature = Temperature.Substring(0, p) + “℃”;minTemperature = Temperature.Substring(p + 1, Temperature.Length – p – 1);}}public event PropertyChangedEventHandler PropertyChanged;(string name){PropertyChangedEventHandler handler = PropertyChanged;if(handler!=null){handler(this, new PropertyChangedEventArgs(name));}}}}

Forecast类,这个的主要功能是异步获取数据和解析Xml:

using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net;using System.Net.Http;using System.Xml;using System.Xml.Linq;using System.IO;using System.ComponentModel;using Windows.UI.Xaml;using Windows.Storage;namespace WeatherForecastDIY{天气预报类/// </summary>class Forecast:INotifyPropertyChanged{currentTemperature;City{set{if (city != value){city = value;OnpropertyChanged(“City”);}}get{return city;}}public string CurrentTemperature//实时气温{set{if (currentTemperature != value){currentTemperature = value;OnpropertyChanged(“CurrentTemperature”);}}get{return currentTemperature;}}public ForecastPeriod TodayForecast//当天的天气{set;get;}public Visibility Vis{set{if(value!=vis){vis = value;OnpropertyChanged(“Vis”);}}get{return vis;}}public Forecast(string CityName){City = CityName;ForecastList = new ObservableCollection<ForecastPeriod>();TodayForecast=new ForecastPeriod();}public Forecast(){City = “成都”;//默认是成都萌萌哒ForecastList = new ObservableCollection<ForecastPeriod>();TodayForecast = new ForecastPeriod();}异步获取天气预报(){//异步一开始设置进度条可见Vis = Visibility.Visible;//局部变量ObservableCollection<ForecastPeriod> newForecastList = new ObservableCollection<ForecastPeriod>();//异步获取数据,很多童鞋问为啥uri要加DateTime,那是因为如果uri是一样的,不知道哪里的缓存就不让你更新数据Uri uri = new Uri(“http://api.map.baidu.com/telematics/v3/weather?location=” + City + “&output=xml&ak=HyXsPscHkQOfR0nxyUmYrV8l&date=” + System.DateTime.Now.ToString());HttpClient httpClient = new HttpClient();HttpResponseMessage response = await httpClient.GetAsync(uri);response.EnsureSuccessStatusCode();Stream stream = await response.Content.ReadAsStreamAsync();//搞到XlmXElement now = XElement.Load(stream);//用完释放好习惯httpClient.Dispose();(now.Element(“status”).Value.Equals(“success”)){//往下走啊往下走now = now.Element(“results”).Element(“weather_data”).Element(“date”);//这天的天气ForecastPeriod newForecastPeriod = new ForecastPeriod();//那一排Xml天气数据居然是并列的,解析解析while (now != null){//获取当前节点的名字string nowName=now.Name.LocalName;if (nowName.Equals(“date”)){string tmp = now.Value;//当日期的长度大于2,就是当天的日期,那个有实时天气的(这个半夜会迷之失效,为啥问度娘)if (tmp.Length > 2){newForecastPeriod.Date = tmp.Substring(0, 2);//简单的说就是在提取实时气温int p = -1;for (int i = 0; i < tmp.Length; i++) if (tmp[i] == ‘:’) { p = i; break; }CurrentTemperature = tmp.Substring(p + 1, tmp.Length – p – 3);}elsenewForecastPeriod.Date = now.Value;}else if (nowName.Equals(“dayPictureUrl”))newForecastPeriod.DayPictureUrl = now.Value;else if (nowName.Equals(“weather”))newForecastPeriod.Weather = now.Value;else if (nowName.Equals(“wind”))newForecastPeriod.Wind = now.Value;else if (nowName.Equals(“temperature”)){//每次到气温的时候就结束了一天的预报,把这一天的情况加入集合newForecastPeriod.Temperature = now.Value;newForecastList.Add(newForecastPeriod);newForecastPeriod = new ForecastPeriod();}now = (XElement)now.NextNode;}//终于解析完了(长吁一口气)//接下来就是赋值了TodayForecast.Weather = newForecastList[0].Weather;TodayForecast.Wind = newForecastList[0].Wind;TodayForecast.MaxTemperature = newForecastList[0].MaxTemperature;TodayForecast.MinTemperature = newForecastList[0].MinTemperature;foreach (ForecastPeriod forecastPeriod in newForecastList)ForecastList.Add(forecastPeriod);}else{//熊孩子一定乱输的城市,然后被度娘给了Error//所以要纠正过来,还是默认成都萌萌哒City = “成都”;//改应用数据ApplicationDataContainer localsetting = Windows.Storage.ApplicationData.Current.LocalSettings;localsetting.Values[“City”] = City;//这叫不叫递归调用呢?反正再来一次getForecastAsync();}//终于异步完了,关掉进度条Vis = Visibility.Collapsed;}没什么用的函数,懒得改了,,大家明白那个意思就好(){getForecastAsync();}public event PropertyChangedEventHandler PropertyChanged;绑定(string name){PropertyChangedEventHandler handler = PropertyChanged;if (handler != null){handler(this, new PropertyChangedEventArgs(name));}}}}那风再温柔。太深的流连便成了一种羁绊,

Windows Phone 8.1 开发实例 天气预报

相关文章:

你感兴趣的文章:

标签云: