Dailyforecast.cs:
/* "date": "2015-07-02", //Ô¤±¨ÈÕÆÚ "astrosr": "04:50", //ÈÕ³öʱ¼ä "astross": "19:47" //ÈÕÂäʱ¼ä "txt_d": "Çç", //°×ÌìÌìÆø×´¿öÃèÊö "txt_n": "Çç" //Ò¹¼äÌìÆø×´¿öÃèÊö "hum": "14", //Ïà¶Ôʪ¶È£¨%£© "pop": "0", //½µË®¸ÅÂÊ "tmpmax": "34", //×î¸ßÎÂ¶È "tmpmin": "18" //×îµÍÎÂ¶È "winddir": "¶«ÄÏ·ç", //·çÏò "windsc": "3-4", //·çÁ¦ */ public class Dailyforecast { public string Date { get; set; } public string Astrosr { get; set; } public string Astross { get; set;} public string Txt_d { get; set; } public string Txt_n { get; set; } public string Hum { get; set; } public string Pop { get; set; } public string Tmpmax { get; set; } public string Tmpmin { get; set; } public string Winddir { get; set; } public string Windsc { get; set; } } View CodeÎÒÃÇÀ´¿´½ØÈ¡µÄÕâÒ»¶ÎJSONÊý¾Ý£º
JObject jsonobj = JObject.Parse(result);JArray jsonarry = JArray.Parse(jsonobj["HeWeather data service 3.0"].ToString());
JToken suggestion = jsonarry[0]["suggestion"];suggestions.Comfbrf = suggestion["comf"]["brf"].ToString();
½èÖúÉÏÃ漸ÐдúÂë¾Í¿ÉÒÔÈ¡µ½µÚÈýÐеġ°½ÏÊæÊÊ¡±Èý¸ö×ÖÁË£¬Ïñjsonarry[0]["suggestion"]["comf"]["brf"]ÕâÑùÀ´È¡ÏàÓ¦µÄÊý¾Ý»¹ÊÇÏ൱·½±ãµÄ£¬¶¯ÊÖÊÔÊÔ£¬²»Ì«Äѵġ£ÎÒÕâÀïÖ®ËùÒÔÕâô¶à´úÂ룬ÊÇΪÁËÒª½âÎö³öºÜ¶àµÄJSONÊý¾Ý¡£¡£¡£¡£À´¿´ÔËÐнá¹û£º£¨·Ï»°£º²éѯ½á¹ûÒÀ´ÎÊǵ±Ç°ÌìÆø£¬FilpView¿Ø¼þչʾµÄδÀ´Ê®ÌìÌìÆø£¬Ò»Ð©ÌìÆøÖ¸Êý£©
3.²©¿ÍÔ°RSS£¨£©µÄ½âÎö
ÔÚUWPÀïÃ棬Windows.Web.SyndicationÃüÃû¿Õ¼ä¶Ô·ÃÎÊRSSÔ´ÌṩÁ˱ȽϺõÄÖ§³Ö£¬È»¶øÕâ´Î¶Ô²©¿ÍÔ°RSSµÄ½âÎö²¢Ã»ÓвÉÓÃÕâ¸öÃüÃû¿Õ¼äϵÄÀ࣬¶øÊÇÖ±½Ó½âÎöXMLÎĵµÀ´µÄ¡£¸öÈ˸оõÔÚ½âÎöµÄʱºò»¹ÊÇÓöµ½ÁËһЩ¿ÓµÄ£¬ÀïÃæòËÆÁôÁËһЩ¿Õ¸ñµÈ¶«Î÷£¬·´ÕýSelectNodesµÈµÄÄǸöXpathÔõôд¶¼²»Ì«¶Ô£¬×îºóÖ±½ÓÓÃGetElementsByTagNameÁË£¬´ËÍ´ÊÔ¹ý¾ÍÖªµÀ¡£¡£¡£ÎÒÃǵÄHttpClientÀà²ÉÓõÄÊÇWindows.Web.HttpÃüÃû¿Õ¼äϵģ¬XmlDocumentÔòÀ´×ÔWindows.Data.Xml.DomÃüÃû¿Õ¼ä¡£»¹ÊÇÏñÒÔÇ°Ìáµ½µÄ£¬XML½âÎöÓú᱿ìËÙ¼àÊÓ¡°¸ù¾ÝÇé¿ö¶¯Ì¬µ÷Õûһϣ¬²»Ì«Äѵġ£ok£¬Ö±½ÓÌù´úÂ룬²»×ö¹ý¶à½âÊÍ¡£
ǰ̨xaml:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <ListView x:Name="listview" ItemClick="listview_ItemClick" IsItemClickEnabled="True"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical" Margin="10"> <TextBlock TextWrapping="Wrap" Text="{Binding Title}" FontSize="18"/> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding AuthorName}" FontSize="15"/> <TextBlock Text="{Binding PublishedTime}" FontSize="15" Margin="30,0,10,0"/> </StackPanel> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> <WebView x:Name="wv" Grid.Column="1"/> </Grid>ºǫ́´úÂ룺£¨·Ï»°£º°ÑInitData·½·¨·ÅÔÚMainPageµÄ¹¹Ô캯ÊýÀïÃæµ÷Óã©
private async void InitData() { HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible;MSIE 10.0;Windows NT 6.2;WOW64;Trident/6.0)"); HttpResponseMessage response = await httpClient.GetAsync(new Uri("")); response.EnsureSuccessStatusCode(); string result = await response.Content.ReadAsStringAsync(); List<Article> articlelist = new List<Article>(); XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(result); IXmlNode xn = xdoc.ChildNodes.Item(1); // feed XmlElement topxe = (XmlElement)xn; XmlNodeList nodelist = topxe.GetElementsByTagName("entry"); foreach (IXmlNode node in nodelist) { XmlNodeList xeIdnodelist = ((XmlElement)node).GetElementsByTagName("id"); XmlNodeList xeTitlenodelist = ((XmlElement)node).GetElementsByTagName("title"); XmlNodeList xePublishednodelist = ((XmlElement)node).GetElementsByTagName("published"); XmlNodeList xecontentnodelist = ((XmlElement)node).GetElementsByTagName("content"); XmlNodeList xeAuthornodelist = ((XmlElement)node).GetElementsByTagName("author"); string id = xeIdnodelist.Item(0).InnerText; string title = xeTitlenodelist.Item(0).InnerText.Split('-')[0]; string published = xePublishednodelist.Item(0).InnerText; string content = xecontentnodelist.Item(0).InnerText; string authorname = xeAuthornodelist.Item(0).ChildNodes.Item(1).InnerText; articlelist.Add(new Article() { Id = id, Title = title, AuthorName = authorname, PublishedTime = published, Content = content }); } listview.ItemsSource = articlelist; } private void listview_ItemClick(object sender, ItemClickEventArgs e) { Article article = e.ClickedItem as Article; wv.NavigateToString(article.Content); } ArticleÀࣺ public class Article { public string Id { get; set; } public string Title { get; set; } public string AuthorName { get; set; } public string PublishedTime { get; set; } public string Content { get; set; } } View CodeÔËÐнá¹û£º£¨·Ï»°£ºÎÒÃÇÓÃListView¿Ø¼þÀ´ÏÔʾÎÄÕµıêÌâµÈÐÅÏ¢£¬ÎÄÕµÄÄÚÈÝÓÉWebView¿Ø¼þÀ´Õ¹Ê¾£©
4.UWPµ÷ÓÃ×Ô¼ºÊµÏÖµÄWeb API
ÎÒÃÇÀ´ÀûÓÃASP.NET Web APIʵÏÖÒ»¸ö¼òµ¥µÄAPI½Ó¿Ú¹©UWP¿Í»§¶ËÀ´µ÷Óá£ÓÉÓÚ±¾È˵ÄASP.NET¼¼ÊõÓÐÏÞ£¬ËùÒÔÕâ¸öAPI¼¸ºõÊÇ¿¿Visual StudioÀ´×Ô¶¯Éú³É£¬ÎÒÃǵÄÊý¾Ý²ÉÓÃDatabase FirstµÄ·½Ê½À´ÊµÏÖ¡£
Firstly£¬Ð½¨Ò»¸öASP.NET WebÓ¦ÓóÌÐò£¬È»ºóÀ´µ½ÏÂͼ£¬Îª¼òµ¥Æð¼û£¬Ñ¡Ôñ¡°Empty¡±Ä£°å£¬È»ºóÑ¡ÖС°Web API¡±£¬µã»÷¡°È·¶¨¡±£¬ok¡£
Secondly£¬ÓÒ»÷ÏîÄ¿µÄ¡°Models¡±Îļþ¼Ð£¬ÒÀ´Î£¬¡°Ìí¼Ó¡±–>"н¨Ïî"£¬ÔÚÓÒ±ßÑ¡Ôñ¡°Êý¾Ý¡±£¬È»ºóÑ¡Ôñ¡°ADO.NET ʵÌåÊý¾ÝÄ£ÐÍ¡±£¬Ãû³Æ´¦ÃüÃû£¬µã»÷¡°Ìí¼Ó¡±£¬ok
È»ºóÎÒÃÇÀ´µ½ÏÂͼ£¬Ñ¡Ôñ¡°À´×ÔÊý¾Ý¿âµÄCode First¡±£¬µã»÷¡°ÏÂÒ»²½¡±
½Ó×ÅÊÇÏÂͼ
µã»÷н¨Á¬½Ó£¬³öÏÖÏÂͼ£¬·þÎñÆ÷Ãû¡°.¡±±íʾ±¾»ú£¬È»ºóµÇ½µ½·þÎñÆ÷£¬Ñ¡ÔñÒªÁ¬½ÓµÄÊý¾Ý¿â£¬²âÊÔÁ¬½Ó£¬Á¬½Ó³É¹¦£¬µã»÷¡°È·¶¨¡°¼´¿É
È»ºóÊÇ´Ëͼ£¬ÎÒÃÇÕâ´ÎÖ»Óõ½Books±í£¬ËùÓоÍֻѡÖÐËüÁË¡£µã»÷¡°Íê³É¡±Ö®ºó£¬»áÔÚModelsÎļþ¼ÐÏÂÃæÉú³ÉÐí¶àµÄÎļþ£¬×ÔÐв鿴¡£¡£¡£
Ç°ÃæÕâЩ²½Öè˵ÆðÀ´±È½ÏÂé·³£¬Ò²¿ÉÄÜ»¹Ã»ËµÇå³þ£¬ÓÉÓÚÎÒ֮ǰÒѾÍê³ÉÁ˱àÂ룬ÔټǼµÄ²©¿Í£¬Ò²»á©µôһЩÄÚÈÝ£¬ÕâЩÈç¹ûÊÇ×Ô¼º²Ù×÷ÊìÁË£¬»áºÜ¼òµ¥µÄ¡£Êý¾Ý»¹¿ÉÒÔ²ÉÓÃCode FirstµÈ·½Ê½£¬£¬£¬£¬£¬²»ÏêÊöÁË¡£
Then£¬ÓÒ»÷¡°ControllersÎļþ¼Ð¡±£¬µã»÷Ìí¼Ó¿ØÖÆÆ÷£¬ÈçÏÂͼ£¬µã»÷¡°Ìí¼Ó¡±
Ö®ºóÊÇÕâ¸ö£¬Ñ¡ÔñÊý¾ÝÉÏÏÂÎÄÀàºÍÄ£ÐÍÀ࣬ȻºóÔÙµã»÷¡°Ìí¼Ó¡±
¡¡