2012-02-14 62 views
0

我目前正試圖建立在Windows Phone有點RSS閱讀器應用程序7.讀取多個RSS源

到目前爲止,我能夠在不同的全景圖顯示單一飼料和它工作正常。

但是在我的MainPage上,我想顯示來自多個RSS源的最新消息。

例如,我在不同的頁面上有feed1,feed2,我想在我的主頁上顯示來自feed1和feed2(或更多)的最新消息的標題。

這是我一直在使用我的單身飼料代碼

Mainpage_Loaded:

WebClient wc = new WebClient(); 
wc.DownloadStringAsync(new Uri("feed-goes-here")); 
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); 

然後:

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 

      if (e.Error != null) return; 

      XElement xmlItems = XElement.Parse(e.Result); 

      listBox1.ItemsSource = from x in xmlItems.Descendants("item") 
            where x.Element("enclosure") != null && x.Element("description") != null 
            select new RSSitem 
            { 
             Description = x.Element("description").Value.Substring(0,100)+"...", 
             Title = x.Element("title").Value, 
             ImageSource = x.Element("enclosure").FirstAttribute.Value 
            }; 

     } 

我已經有很多的方法來測試解決我的問題,但仍然找不到答案。

所以我的問題是:我如何顯示在列表框上,從同一頁上的2個不同的飼料最近的新聞? 謝謝你的幫助和你的時間。

+0

問題在哪裏? – onof 2012-02-14 15:25:26

回答

0

首先,我會建議增加一個日期屬性的RSSItem:

public class RSSItem 
{ 
    public DateTime Date { get; set; } 
    public string Description { get; set; } 
    public string Title { get; set; } 
    public string ImageSource { get; set; } 
} 

這樣,當你下載你的兩個RSS源,你可以編織在一起並按日期排序:

private IEnumerable<RSSItem> Weave(List<RSSItem> feed1, List<RSSItem> feed2) 
{ 
    return feed1.Concat(feed2) 
     .OrderByDescending(i => i.Date) 
     .Take(10); // Grab the ten most recent entries 
} 

用法:

using System.Linq; // Don't forget to add using statement for System.Linq 

public class RssReader 
{ 
    private List<IEnumerable<RSSItem>> _feeds = new List<IEnumerable<RSSItem>>(); 

    public void Download() 
    { 
     WebClient wc = new WebClient(); 
     wc.DownloadStringCompleted += this.FeedDownloaded; 
     wc.DownloadStringAsync(new Uri("feed-goes-here")); 
    } 

    private void FeedDownloaded(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Error != null) return; 

     var xml = XElement.Parse(e.Result); 
     var feed = from x in xml.Descendants("item") 
        where x.Element("enclosure") != null && x.Element("description") != null 
        select new RSSItem() 
        { 
         Date = DateTime.Parse(x.Element("date").Value), 
         Title = x.Element("title").Value, 
         ImageSource = x.Element("enclosure").FirstAttribute.Value 
        }; 

     _feeds.Add(feed); 

     if (_feeds.Count == 2) 
     { 
      var result = this.Weave(_feeds[0], _feeds[1]); 

      // Assign result to the list box's ItemSource 
      // Or better, use data binding. 
     } 
    } 

    /// <summary> 
    /// Combines the two feeds, sorts them by date, and returns the ten most recent entries. 
    /// </summary> 
    private IEnumerable<RSSItem> Weave(IEnumerable<RSSItem> feed1, IEnumerable<RSSItem> feed2) 
    { 
     return feed1.Concat(feed2) 
      .OrderByDescending(i => i.Date) 
      .Take(10); 
    } 
}