Metro学习笔记+心得+体会(三)
五月快要结束了,还有14天就要会考,告诉自己:加油!
这是本月最后一篇教程,欢迎拍砖!
本篇博客要讲的是一个博客阅读器,我学习msdn的例子http://msdn.microsoft.com/zh-cn/LIbrary/br211380.aspx#Y25237,自已写了个关于我的博客的阅读器。
原理很简单,SyndicationClient类,以下是主要代码:
1 public class FeedData 2 { 3 public string Title { set; get; } 4 public string Description { get; set; } 5 public DateTime PubDate { get; set; } 6 7 private List<FeedItem> _Items = new List<FeedItem>(); 8 public List<FeedItem> Items 9 { 10 get 11 { 12 return this._Items; 13 } 14 } 15 } 16 public class FeedItem 17 { 18 public string Title { get; set; } 19 public string Author { get; set; } 20 public string Content { get; set; } 21 public DateTime PubDate { get; set; } 22 public Uri Link { get; set; } 23 24 } 25 26 public class FeedDataSource 27 { 28 private ObservableCollection<FeedData> _Feeds=new ObservableCollection<FeedData>(); 29 public ObservableCollection<FeedData> Feeds 30 { 31 get 32 { 33 return this._Feeds; 34 } 35 } 36 37 public async Task GetFeedsAsync() 38 { 39 /* 40 Task<FeedData> feed1 = 41 GetFeedAsync("http://windowsteamblog.com/windows/b/developers/atom.aspx"); 42 Task<FeedData> feed2 = 43 GetFeedAsync("http://windowsteamblog.com/windows/b/windowsexperience/atom.aspx"); 44 Task<FeedData> feed3 = 45 GetFeedAsync("http://windowsteamblog.com/windows/b/extremewindows/atom.aspx"); 46 Task<FeedData> feed4 = 47 GetFeedAsync("http://windowsteamblog.com/windows/b/business/atom.aspx"); 48 Task<FeedData> feed5 = 49 GetFeedAsync("http://windowsteamblog.com/windows/b/bloggingwindows/atom.aspx"); 50 Task<FeedData> feed6 = 51 GetFeedAsync("http://windowsteamblog.com/windows/b/windowssecurity/atom.aspx"); 52 Task<FeedData> feed7 = 53 GetFeedAsync("http://windowsteamblog.com/windows/b/springboard/atom.aspx"); 54 Task<FeedData> feed8 = 55 GetFeedAsync("http://windowsteamblog.com/windows/b/windowshomeserver/atom.aspx"); 56 // There is no Atom feed for this blog, so we use the RSS feed. 57 Task<FeedData> feed9 = 58 GetFeedAsync("http://windowsteamblog.com/windows_live/b/windowslive/rss.aspx"); 59 Task<FeedData> feed10 = 60 GetFeedAsync("http://windowsteamblog.com/windows_live/b/developer/atom.aspx"); 61 Task<FeedData> feed11 = 62 GetFeedAsync("http://windowsteamblog.com/ie/b/ie/atom.aspx"); 63 Task<FeedData> feed12 = 64 GetFeedAsync("http://windowsteamblog.com/windows_phone/b/wpdev/atom.aspx"); 65 Task<FeedData> feed13 = 66 GetFeedAsync("http://windowsteamblog.com/windows_phone/b/wmdev/atom.aspx"); 67 68 this.Feeds.Add(await feed1); 69 this.Feeds.Add(await feed2); 70 this.Feeds.Add(await feed3); 71 this.Feeds.Add(await feed4); 72 this.Feeds.Add(await feed5); 73 this.Feeds.Add(await feed6); 74 this.Feeds.Add(await feed7); 75 this.Feeds.Add(await feed8); 76 this.Feeds.Add(await feed9); 77 this.Feeds.Add(await feed10); 78 this.Feeds.Add(await feed11); 79 this.Feeds.Add(await feed12); 80 this.Feeds.Add(await feed13); 81 */ 82 Task<FeedData> feed12 = 83 GetFeedAsync("http://feed.cnblogs.com/blog/u/116459/rss"); 84 85 Task<FeedData> feed13 = 86 GetFeedAsync("http://feed.cnblogs.com/blog/sitehome/rss"); 87 this.Feeds.Add(await feed13); 88 this.Feeds.Add(await feed12); 89 } 90 91 private async Task<FeedData> GetFeedAsync(string feedUriString) 92 { 93 SyndicationClient client = new SyndicationClient(); 94 Uri feedUri = new Uri(feedUriString); 95 96 try 97 { 98 SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri); 99 FeedData feedData = new FeedData(); 100 feedData.Title = feed.Title.Text; 101 if (feed.Subtitle.Text != null) 102 { 103 feedData.Description = feed.Subtitle.Text; 104 } 105 106 feedData.PubDate = feed.Items[0].PublishedDate.DateTime; 107 108 foreach (SyndicationItem item in feed.Items) 109 { 110 FeedItem feedItem = new FeedItem(); 111 feedItem.Title = item.Title.Text; 112 feedItem.PubDate = item.PublishedDate.DateTime; 113 feedItem.Author = item.Authors[0].Name; 114 115 if (feed.SourceFormat == SyndicationFormat.Atom10) 116 { 117 feedItem.Content = item.Content.Text; 118 feedItem.Link = item.Links[0].Uri; 119 } 120 feedData.Items.Add(feedItem); 121 } 122 return feedData; 123 } 124 catch (Exception) 125 { 126 return null; 127 } 128 } 129 130 }
完整的源码请鉴源码,由于时间关系,本博客不是很完整,本人也会在后续系列中修正。大家共同学习!
源码地址:
https://skydrive.live.com/redir?resid=F408A3D3DF420664!183&authkey=!ABl7sbIuIN_gzU8