跟着bob学win10 ump 学习笔记: lesson57 Weather

视频地址:

https://channel9.msdn.com/Series/Windows-10-development-for-absolute-beginners/UWP-060-UWP-Weather-Testing-Location-in-the-Phone-Emulator?ocid=EntriesInArea

json2csharp  工具地址 http://json2csharp.chahuo.com/ 视频是国外网站 http://json2csharp.com 访问太慢了。

第一步:处理下主界面MainPage.xaml

<Page
    x:Class="UMPWeather.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UMPWeather"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel>
        <Button Name="GetWeatherButton" Content="GetWeather" Click="GetWeatherButton_Click"></Button>
        <TextBlock Name="MyTextBlock"></TextBlock>
        <Image Name="WeatherImage"></Image>
    </StackPanel>
</Page>

第二步:工具=》NuGet包管理器=》管理解决方案的NUGet包=》搜索“http”=》添加microsoft.net.http程序包

第三步:通过webAPI获取天气数据,视频里面用的是openWeatherMap,外国服务器这么慢,用国内的咯,查了下,只有个中国天气网,还有腾讯百度提供的天气API,但是全都要注册,麻烦,我想就直接网页抓取就好了。于是写了个方法来获取这些数据

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Net.Http;
  7 using System.Text.RegularExpressions;
  8 using Windows.UI.Xaml.Controls;
  9
 10 namespace UMPWeather
 11 {
 12     public class WeatherManager
 13     {
 14
 15         private static async Task<List<string>> getWeatherDataByBaiduAsync()
 16         {
 17
 18             try
 19             {
 20                 List<string> weatherList = new List<string>();
 21                 var http = new HttpClient();
 22                 var responseString = await http.GetStringAsync("http://www.baidu.com/s?wd=本地天气");
 23                 const string regexWeatherString = @"op_weather4_twoicon_today([\s\S]+?)</a>";//匹配
 24                 MatchCollection myMatch = Regex.Matches(responseString, regexWeatherString);
 25                 //这个获取到3条数据   第二条是我们要的。
 26                 #region 获得的数据
 27
 28                 /*op_weather4_twoicon_today OP_LOG_LINK" target="_blank" href=‘http://www.baidu.com/link?url=IIr1VEd89cnNmtp5DztMt2B_t3rXTE2tjBV8TXaqSThssbE0vKYCwiJiNjO2pfNj3R2FuLM13eV64pcgSQlZ8q‘ data-click=‘{"url":"http://www.weather.com.cn/weather/101200501.shtml"}‘ weath-bg=‘cloudy‘ weath-eff=‘[]‘>
 29                         <p class="op_weather4_twoicon_date">
 30
 31
 32                                                     周三 05月04日 农历三月廿八 (实时:27℃)
 33                                 </p>
 34
 35                     <div class="op_weather4_twoicon_icon" style="background:url(http://s1.bdstatic.com/r/www/aladdin/img/new_weath/bigicon/3.png);*background:none;*filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://s1.bdstatic.com/r/www/aladdin/img/new_weath/bigicon/3.png)"></div>
 36
 37                     <p class="op_weather4_twoicon_temp">29 ~ 20<sup>℃</sup></p>
 38                     <p class="op_weather4_twoicon_weath"
 39                     title="">
 40
 41                                                                 阴
 42
 43                     </p>
 44                     <p class="op_weather4_twoicon_wind">微风</p>
 45
 46                                                             <p class="op_weather4_twoicon_pm25">实时空气质量:
 47                                                 <em style="background:#afdb00"><b>75</b>&nbsp;良</em>
 48                                             </p>
 49                                 </a>*/
 50                 #endregion
 51                 string ResultString = myMatch[1].ToString();
 52                 //一共5个p  分别对应date,temp,weath,wind,pm25
 53                 const string regexPString = @"<p([\s\S]+?)</p>";//匹配全部的段落
 54                 myMatch = Regex.Matches(ResultString, regexPString);
 55
 56                 foreach (var item in myMatch)
 57                 {
 58                     //去除文本中的全部空格
 59                     string a = item.ToString();
 60                     a.Replace(" ", "");
 61
 62                     //将今天的天气数据放在天气清单里面
 63                     weatherList.Add(a);
 64                 }
 65                 return weatherList;
 66             }
 67             catch (Exception )
 68             {
 69
 70                throw ;
 71
 72             }
 73         }
 74         public static async Task<Weather> GetWeatherAsync(Weather weatherToday)
 75         {
 76             List<string> weatherList = await getWeatherDataByBaiduAsync();
 77
 78             if (weatherList.Count == 5)
 79             {
 80                 weatherToday.Data = weatherList[0];
 81                 weatherToday.Temp = weatherList[1];
 82                 weatherToday.Weath = weatherList[2];
 83                 weatherToday.Wind = weatherList[3];
 84                 weatherToday.PM25 = weatherList[4];
 85
 86             }
 87             return weatherToday;
 88         }
 89
 90     }
 91     public class Weather
 92     {
 93         public string Data { get; set; }
 94         public string Temp { get; set; }
 95         public string Weath  { get; set; }
 96         public string Wind { get; set; }
 97         public string PM25 { get; set; }
 98
 99
100     }
101 }

第四步:调用看下结果

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Text;
//“空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 上有介绍

namespace UMPWeather
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {

        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void GetWeatherButton_Click(object sender, RoutedEventArgs e)
        {
            var weatherToday = await WeatherManager.GetWeatherAsync(new Weather());
            WeatherListBox.Items.Add(weatherToday.Data);
            WeatherListBox.Items.Add(weatherToday.Temp);
            WeatherListBox.Items.Add(weatherToday.Weath);
            WeatherListBox.Items.Add(weatherToday.Wind);
            WeatherListBox.Items.Add(weatherToday.PM25);
        }
    }
}

这个是程序运行结果

还要做些后续处理,不过不管怎样,数据总算是能获取到了。

时间: 2024-08-01 09:14:24

跟着bob学win10 ump 学习笔记: lesson57 Weather的相关文章

《从零开始学Swift》学习笔记(Day 57)——Swift编码规范之注释规范:

<从零开始学Swift>学习笔记(Day 57)--Swift编码规范之注释规范:文件注释.文档注释.代码注释.使用地标注释 原创文章,欢迎转载.转载请注明:关东升的博客 前面说到Swift注释的语法有两种:单行注释(//)和多行注释(/*...*/).这里来介绍一下他们的使用规范. 文件注释 文件注释就在每一个文件开头添加注释,文件注释通常包括如下信息:版权信息.文件名.所在模块.作者信息.历史版本信息.文件内容和作用等. 下面看一个文件注释的示例: /* Copyright (C) 201

《从零开始学Swift》学习笔记(Day3)——Swift2.0之后增加的关键字

原创文章,欢迎转载.转载请注明:关东升的博客 看了之前的学习笔记知道了什么是关键字,现在提示各位在Swift 2.0之后增加defer.guard.repeat.catch.rethrows.throw.throws和try关键字,其中repeat关键字替代do - while循环中的do,即中repeat -while循环.而do关键字用于错误处理.catch.rethrows.throw.throws和try是错误处理关键字. 错误处理是Swift 2.0之后增加新内容. 欢迎关注关东升新浪

《从零開始学Swift》学习笔记(Day5)——我所知道的标识符和keyword

?? Swift 2.0学习笔记(Day5)--我所知道的标识符和keyword   原创文章,欢迎转载.转载请注明:关东升的博客 好多计算机语言都有标识符和keyword,一直没有好好的总结,就是这种用着,如今小小的整理一下Swift中的标识符和keyword. 什么是标识符呢? 标识符就是给变量.常量.方法.函数.枚举.结构体.类.协议等由开发者指定的名字. 事实上.构成标识符的字母是有一定规范的,Swift中命名规则是: 区分大写和小写.Myname与myname是两个不同的标识符: 标识

《从零开始学Swift》学习笔记(Day 11)——数据类型那些事儿?

原创文章,欢迎转载.转载请注明:关东升的博客    在我们学习语言时都会学到这种语言的数据类型,在Swift中数据类型有那些呢?整型.浮点型.布尔型.字符.字符串这些类型是一定有的,其中集合.枚举.结构体.类也是Swift中的数据类型.元组是Swift中特有的. 其他那些类型我们可能多少知道些或是听说过.元组是在Swift中有的,它是什么呢? 它是一种数据结构,在数学中应用广泛.在计算机科学中,元组是关系数据库中的基本概念,元组表中的一条记录,每列就是一个字段.因此在二维表里,元组也称为记录.

《从零开始学Swift》学习笔记(Day4)——用Playground工具编写Swift

原创文章,欢迎转载.转载请注明:关东升的博客 用Playground编写Swift代码目的是为了学习.测试算法.验证想法和可视化看到运行结果,不是为了使最终的程序编译和发布. Playground程序运行①区域是代码编写视图:②区域是运行结果视图:③区域是时间轴视图:④区域是控制台视图,使用print等日志函数将结果输出到控制台,可以通过左下角的      按钮隐藏和显示控制台. 默认情况下时间轴视图是不显示的,可以通过助手编辑器打开Playground时间轴视图.在出现的工具栏中,单击打开助手

《从零開始学Swift》学习笔记(Day 46)——下标重写

原创文章.欢迎转载.转载请注明:关东升的博客 下标是一种特殊属性. 子类属性重写是重写属性的getter和setter訪问器,对下标的重写也是重写下标的getter和setter訪问器. 以下看一个演示样例: class DoubleDimensionalArray { let rows: Int, columns: Int var grid: [Int] init(rows: Int, columns: Int) { self.rows = rows self.columns = column

《从零开始学Swift》学习笔记(Day 49)——扩展声明

?? 原创文章,欢迎转载.转载请注明:关东升的博客 声明扩展的语法格式如下: extension 类型名 { //添加新功能 } 声明扩展的关键字是extension,"类型名"是Swift中已有的类型,包括类.结构体和枚举,但是我们仍然可以扩展整型.浮点型.布尔型.字符串等基本数据类型,这是因为这些类型本质上也是结构体类型.打开Int的定义如下: struct Int : SignedInteger { init() init(_ value: Int) static func co

[学] GO 语言学习笔记(一)

1. 变量 var  <名称>   类型 2. 初始化 var v1 int  = 10 v3 := 10 // v3 不可以是已经声明过的变量 3. 忽略返回值 _, _, Nick = GetName() 4. 常量 & iota const Pi   float64 = 3.1415 const ( size  int64 = 1024 eof = -1 ) const  a, b, c = 3, 5, "XX" const ( v1 = 1 <<

《从零開始学Swift》学习笔记(Day 57)——Swift编码规范之凝视规范:文件凝视、文档凝视、代码凝视、使用地标凝视

原创文章.欢迎转载.转载请注明:关东升的博客 前面说到Swift凝视的语法有两种:单行凝视(//)和多行凝视(/*...*/).这里来介绍一下他们的使用规范. 1.文件凝视 文件凝视就在每个文件开头加入凝视,文件凝视通常包含例如以下信息:版权信息.文件名称.所在模块.作者信息.历史版本号信息.文件内容和作用等. 以下看一个文件凝视的演示样例: /* Copyright (C) 2015 Eorient Inc. All Rights Reserved. See LICENSE.txt for t