WP8.1开发:简单天气预报应用(转)
今天小梦给大家分享一个简单的天气预报应用源码:调用的是百度API.整个应用都没有什么难点.只是一个简单的网络请求和json数据处理.在WP8.1有小娜的情况下,天气预报应用还有意义吗?我认为还是有点意义的,至少数据更详细,而要想要用户喜欢,必须有比小娜更人性化的提醒和精美的UI.UI必须要精美.当然小梦今天分享的UI很简单.大家可以根据自己的想象去处理UI.之所以分享出来,是分享JSON的数据处理.在WP8.1开发XML读取中央气象城市代码文件中,分享了XML文件和JSON数据解析的方法,不过其中是直接处理的JSON数据,今天分享的是利用对象序列化JSON数据.当然,如有不当或错误,欢迎指正.
JSON数据本身:
{
"error": 0,
"status": "success",
"date": "2014-09-13",
"results": [
{
"currentCity": "西安市",
"pm25": "25",
"index": [
{
"title": "穿衣",
"zs": "较舒适",
"tipt": "穿衣指数",
"des": "建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"
},
{
"title": "洗车",
"zs": "不宜",
"tipt": "洗车指数",
"des": "不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"
},
{
"title": "旅游",
"zs": "一般",
"tipt": "旅游指数",
"des": "温度适宜,有微风同行,但较强降雨的天气将给您的出行带来很多的不便,若坚持旅行建议带上雨具。"
},
{
"title": "感冒",
"zs": "较易发",
"tipt": "感冒指数",
"des": "天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"
},
{
"title": "运动",
"zs": "较不宜",
"tipt": "运动指数",
"des": "有较强降水,建议您选择在室内进行健身休闲运动。"
},
{
"title": "紫外线强度",
"zs": "最弱",
"tipt": "紫外线强度指数",
"des": "属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"
}
],
"weather_data": [
{
"date": "周六 09月13日 (实时:16℃)",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/zhongyu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/zhongyu.png",
"weather": "中雨",
"wind": "西南风微风",
"temperature": "15℃"
},
{
"date": "周日",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/zhongyu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/zhongyu.png",
"weather": "中雨",
"wind": "西南风微风",
"temperature": "18 ~ 15℃"
},
{
"date": "周一",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/zhongyu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/zhongyu.png",
"weather": "中雨",
"wind": "西南风微风",
"temperature": "19 ~ 15℃"
},
{
"date": "周二",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/zhongyu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/zhongyu.png",
"weather": "中雨",
"wind": "西南风微风",
"temperature": "17 ~ 15℃"
}
]
}
]
}
我们根据JSON数据构建对象:(对象名称最好和数据名称保持一致)
public class WeatherData
{
public int error { get; set; }
public string status { get; set; }
public string date { get; set; }
public List<resultsList> results { get; set; } public class resultsList
{ public string currentCity{ get; set; } public string pm25 { get; set; }
public List<indexList> index { get; set; }
public List<weather_dataList> weather_data { get; set; }
}
public struct indexList
{
public string title { get; set; }
public string zs { get; set; }
public string tipt { get; set; }
public string des { get; set; } } public struct weather_dataList
{
public string date { get; set; }
public string weather { get; set; }
public string wind { get; set; }
public string temperature { get; set; }
public Uri dayPictureUrl { get; set; }
public Uri nightPictureUrl { get; set; }
}
}
应用前台代码:
<Pivot>
<PivotItem Header="天气" >
<ListView Name="listWeather">
<ListView.Header >
<StackPanel>
<TextBlock x:Name="textCity" FontSize="30"></TextBlock>
<TextBlock x:Name="textPm" FontSize="25" Foreground="Orange"></TextBlock>
</StackPanel>
</ListView.Header>
<ListView.ItemTemplate >
<DataTemplate>
<Border Width="360" BorderThickness="2" BorderBrush="#FF2996AE">
<StackPanel >
<TextBlock Text="{Binding date}" FontSize="25"></TextBlock>
<TextBlock Text="{Binding weather}" FontSize="30"></TextBlock>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding dayPictureUrl}" Stretch="Uniform"
Width="60" Height="60"></Image>
<Image Source="{Binding nightPictureUrl}" Stretch="Uniform"
Width="60" Height="60"></Image>
</StackPanel>
<TextBlock Text="{Binding wind}" FontSize="25"></TextBlock>
<TextBlock Text="{Binding temperature }" FontSize="30"></TextBlock> </StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</PivotItem>
<PivotItem Header="指数">
<ListView Name="listIndex">
<ListView.ItemTemplate >
<DataTemplate>
<Border>
<StackPanel >
<TextBlock Text="{Binding tipt }" FontSize="25" Foreground="#FF2996AE"></TextBlock>
<TextBlock Text="{Binding zs}" FontSize="30" Foreground="Green"></TextBlock>
<TextBlock Text="{Binding des }" FontSize="20" TextWrapping="Wrap"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</PivotItem>
</Pivot> </Grid>
<Page.BottomAppBar>
<CommandBar Background="#FF2996AE">
<AppBarButton Icon="Sync" Label="刷新" Click="AppBarButton_Click"/>
</CommandBar>
</Page.BottomAppBar>
后台处理代码:
public sealed partial class MainPage : Page
{
private string latitude = null;
private string longitude = null;
private string responseText = null;
private HttpClient httpClient;
private HttpResponseMessage response; private Weather.WeatherData.resultsList weather; public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required; httpClient = new HttpClient();
var headers = httpClient.DefaultRequestHeaders;//获取每个请求标头的集合
headers.UserAgent.ParseAdd("ie");
headers.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracyInMeters = ;
try
{
Geoposition geoposition = await geolocator.GetGeopositionAsync(
maximumAge: TimeSpan.FromMinutes(),
timeout: TimeSpan.FromSeconds());
latitude = geoposition.Coordinate.Point.Position.Latitude.ToString();
longitude = geoposition.Coordinate.Point.Position.Longitude.ToString();
}
catch (UnauthorizedAccessException)
{
Debug.WriteLine( "无法获取位置信息!");
}
} private async void AppBarButton_Click(object sender, RoutedEventArgs e)
{
response = new HttpResponseMessage(); Uri resourceUri;
String Address = "http://api.map.baidu.com/telematics/v3/weather?location="+longitude+","+latitude+"&output=json&ak=你的KEY";
Uri.TryCreate(Address.Trim(), UriKind.Absolute, out resourceUri); try
{
response = await httpClient.GetAsync(resourceUri); response.EnsureSuccessStatusCode(); responseText = await response.Content.ReadAsStringAsync(); }
catch (Exception ex)
{
Debug.WriteLine("网络请求失败!" + ex.Message.ToString()); } DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(WeatherData));
WeatherData weatherData = obj.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(responseText))) as WeatherData;
weather = weatherData.results.FirstOrDefault();
listWeather.ItemsSource = weather.weather_data;
listIndex.ItemsSource = weather.index;
textCity.Text = weather.currentCity.ToString();
textPm.Text= "PM2.5:"+weather.pm25.ToString(); }
运行效果如下:


完整源码下载:WP8.1天气预报应用.
WP8.1开发:简单天气预报应用(转)的更多相关文章
- WP8.1开发:简单的天气预报应用
今天小梦给大家分享一个简单的天气预报应用源码:调用的是百度API.整个应用都没有什么难点.只是一个简单的网络请求和json数据处理.在WP8.1有小娜的情况下,天气预报应用还有意义吗?我认为还是有点意 ...
- 基于Android开发的天气预报app(源码下载)
原文:基于Android开发的天气预报app(源码下载) 基于AndroidStudio环境开发的天气app -系统总体介绍:本天气app使用AndroidStudio这个IDE工具在Windows1 ...
- 从零开始学Python08作业思路:开发简单的FTP
一,作业要求 开发简单的FTP 1,用户登录 2,上传/下载文件 3,不同用户家目录不同 4,查看当前目录下文件 5,充分使用面向对象 二,程序文件清单 Folder目录:用户上传文件家目录 db目录 ...
- Python开发简单爬虫 - 慕课网
课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序 课程目录 第1章 课程介绍 ...
- 用QtWebKit开发简单的浏览器
用QtWebKit开发简单的浏览器 1.代码实现 工程目录结构如下: AddressBar类包含了地址栏和按钮两个控件,将地址栏回车和按钮点击信号与goToSite()槽连接. 当回车和点击事件发生时 ...
- C# Windows Phone 8 WP8 高级开发,制作不循环 Pivot ,图片(Gallery)导览不求人! 内附图文教学!!
原文:C# Windows Phone 8 WP8 高级开发,制作不循环 Pivot ,图片(Gallery)导览不求人! 内附图文教学!! 一般我们在开发Winodws Phone APP 的时候往 ...
- 从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器
从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器 之前时间一直在使用Caliburn.Micro这种应用了MVVM模式的WPF框架做开发,是时候总结一下了. Calibu ...
- Python开发简单记事本
摘要: 本文是使用Python,结合Tkinter开发简单记事本. 本文的操作环境:ubuntu,Python2.7,采用的是Pycharm进行代码编辑,个人很喜欢它的代码自动补齐功能. 最近很想对p ...
- 用Spring MVC开发简单的Web应用程序
1 工具与环境 借助Eclipse4.3 + Maven3.0.3构建Java Web应用程序.使用Maven内置的servlet 容器jetty,不需手工集成Web服务器到Eclipse.还帮我们自 ...
随机推荐
- Lucene系列六:Lucene搜索详解(Lucene搜索流程详解、搜索核心API详解、基本查询详解、QueryParser详解)
一.搜索流程详解 1. 先看一下Lucene的架构图 由图可知搜索的过程如下: 用户输入搜索的关键字.对关键字进行分词.根据分词结果去索引库里面找到对应的文章id.根据文章id找到对应的文章 2. L ...
- Java如何使用线程解决死锁?
在Java编程中,如何使用线程解决死锁? 以下示例演示如何使用线程的概念解决死锁问题. // from W w w .Y I I b AI.c o M package com.yiibai; impo ...
- Asp.net webapi Owin Get request form data
var formData = await context.Request.ReadFormAsync() as IEnumerable<KeyValuePair<string, strin ...
- Git的杀手级功能之 一 远程仓库
Git的杀手级功能之一:远程仓库 Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上. 一.注册GitHub账号,然后和本地Git仓库来关联免费获得Git远程仓库来学校git的远程仓 ...
- 磁盘格式化/磁盘挂载/手动增加swap空间
4.5/4.6 磁盘格式化 4.7/4.8 磁盘挂载 4.9 手动增加swap空间 磁盘格式化 查看centos7支持的文件系统格式 cat /etc/filesystem,centos7默认的文件 ...
- UNIX环境编程学习笔记(23)——信号处理初步学习
lienhua342014-10-29 1 信号的概念 维基百科中关于信号的描述是这样的: 在计算机科学中,信号(英语:Signals)是 Unix.类 Unix 以及其他 POSIX 兼容的操作系统 ...
- VirtualBox导入已存在的VHD遇到的uuid冲突问题
解决方法: 用命令行进入VirtualBox的安装目录,使用下面的命令: (进入命令行窗口,cmd模式) C:\Program Files\Oracle\VirtualBox>VBoxManag ...
- spring数据连接池配置参数
1.使用jdbc连接,每次使用完毕需要关闭连接. 2.使用数据库连接池就会方便很多,但是如果参数配置不对,也会引起mysql连接数不够,导致mysql挂掉,基本原理是:不使用的连接数及时回收回来,而不 ...
- 如何让form表单在enter键入时不提交
今天在做我的一个小玩意 在线聊天工具的时候 form表单只有一个text和一个button每当我键入enter的时候就刷新.很是郁闷,直接在form上onsumbit=false.才行. 下面是我查询 ...
- yum 安装mysql, yum安装指定版本的mysql
yum安装mysql: 1. 查看有没有安装过 yum list installed MySQL* (有存在要卸载yum remove MySQL*) rpm -qa | grep m ...