码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-05-05 14:27:50      阅读:456      评论:0      收藏:0      [点我收藏+]

标签:

视频地址:

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

这个是程序运行结果

技术分享

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

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

标签:

原文地址:http://www.cnblogs.com/alasq/p/5458591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!