跟着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/ (这是个坑爹的工具,如果json数据里面有数字开头的类,他的转换不会自动修改,我们的csharp类名是不能用数字开头的)

视频是国外网站 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>
View Code

第二步:工具=》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                     //去除文本中的全部空格
 60                     string a = item.ToString();
 61                     a=a.Replace(" ", "");
 62                     a=a.Replace("\n", "");
 63                     a=a.Replace("&nbsp", " ");
 64 
 65                     //匹配所有标签
 66                     const string regexString1 = @"<([\s\S]+?)>";
 67                     //去除标签
 68                     a= Regex.Replace(a, regexString1,"");
 69                     //将今天的天气数据放在天气清单里面
 70                     weatherList.Add(a);
 71                 }
 72                 return weatherList;
 73             }
 74             catch (Exception )
 75             {
 76 
 77                throw ;
 78 
 79             }
 80         }
 81         public static async Task<Weather> GetWeatherAsync(Weather weatherToday)
 82         {
 83             List<string> weatherList = await getWeatherDataByBaiduAsync();
 84 
 85             if (weatherList.Count == 5)
 86             {
 87                 weatherToday.Data = weatherList[0];
 88                 weatherToday.Temp = weatherList[1];
 89                 weatherToday.Weath = weatherList[2];
 90                 weatherToday.Wind = weatherList[3];
 91                 weatherToday.PM25 = weatherList[4];
 92                 
 93             }
 94             return weatherToday;
 95         }
 96         
 97     }
 98     public class Weather
 99     {
100         public string Data { get; set; }
101         public string Temp { get; set; }
102         public string Weath  { get; set; }
103         public string Wind { get; set; }
104         public string PM25 { get; set; }
105 
106 
107     }
108 }
View Code

 

第四步:调用看下结果

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);
        }
    }
}

这个是程序运行结果

 

posted @ 2016-05-05 14:08  啊啦  阅读(468)  评论(0)    收藏  举报