2011-08-01 24 views
2

私は自分のRSSプロジェクトのフィードを取得していますが、ユーザーがコレクションにもっと多くのアイテムを読み込ませる方法がわからないという問題に遭遇しています。現時点では、すべてが一度に読み込まれます。これはいくつかのケースでは間違いありませんが、モバイル接続が遅い場合に備えて、ユーザーがどのように読み込まれるかを選択できるようにしたいと考えています。XML項目をリストボックスに動的に追加する問題、C#

これは借用されたコードなので、混乱の原因となります。

私はこのサンプルにコードを挿入して、一度に30個のアイテムを動的に読み込むことができますか?

のフィードクラス:クラスを設定

namespace MyRSSService 
{ 

public class RssService 
{ 
    /// Gets the RSS items. 
    /// <param name="rssFeed">The RSS feed.</param> 
    /// <param name="onGetRssItemsCompleted">The on get RSS items completed.</param> 
    /// <param name="onError">The on error.</param> 
    public static void GetRssItems(string rssFeed, Action<IList<RssItem>> onGetRssItemsCompleted = null, Action<Exception> onError = null, Action onFinally = null) 
    { 
     WebClient webClient = new WebClient(); 

     // register on download complete event 
     webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e) 
     { 
      try 
      { 
       // report error 
       if (e.Error != null) 
       { 
        if (onError != null) 
        { 
         onError(e.Error); 
        } 
        return; 
       } 

       // convert rss result to model 
       IList<RssItem> rssItems = new List<RssItem>();     
       Stream stream = e.Result; 
       XmlReader response = XmlReader.Create(stream); 
       { 
        SyndicationFeed feeds = SyndicationFeed.Load(response); 

        foreach (SyndicationItem f in feeds.Items) 
        { 
         RssItem rssItem = new RssItem(f.Title.Text, f.Summary.Text, f.PublishDate.ToString(), f.Links[0].Uri.AbsoluteUri); 
         rssItems.Add(rssItem); 
        } 
       }  

       // notify completed callback 
       if (onGetRssItemsCompleted != null) 
       { 
        onGetRssItemsCompleted(rssItems); 
       } 
      } 
      finally 
      { 
       // notify finally callback 
       if (onFinally != null) 
       { 
        onFinally(); 
       } 
      } 
     }; 
     webClient.OpenReadAsync(new Uri(rssFeed)); 
     } 
    } 
    } 

アイテム:

namespace MyRSSService 
{ 
public class RssItem 
{ 
    /// <summary> 
    /// Initializes a new instance of the <see cref="RssItem"/> class. 
    /// </summary> 
    /// <param name="title">The title.</param> 
    /// <param name="summary">The summary.</param> 
    /// <param name="publishedDate">The published date.</param> 
    /// <param name="url">The URL.</param> 
    public RssItem(string title, string summary, string publishedDate, string url) 
    { 
     Title = title; 
     Summary = summary; 
     PublishedDate = publishedDate; 
     Url = url; 

     // Get plain text from html 
     PlainSummary = HttpUtility.HtmlDecode(Regex.Replace(summary, "<[^>]+?>", "")); 
    } 
    public string Title { get; set; } 
    public string Summary { get; set; } 
    public string PublishedDate { get; set; } 
    public string Url { get; set; } 
    public string PlainSummary { get; set; } 
    } 
} 

ページへのバインディングC#のフィードがホストされているサーバーがない限り、フィード

public partial class FeedPage : PhoneApplicationPage 
{ 
    private const string WindowsPhoneBlogPosts = "http://feeds.bbci.co.uk/news/rss.xml"; 

    public FeedPage() 
    { 
     InitializeComponent(); 
     RssService.GetRssItems(WindowsPhoneBlogPosts, (items) => { listbox.ItemsSource = items; }, (exception) => { MessageBox.Show(exception.Message); }, null); 
    } 
} 
+0

ストリーミングXMLドキュメントは、私にはこのような良い考えのようには聞こえません。 –

+0

ありがとう私は推測します。何をお勧めしますか? – Gary

+0

私は知らない、そうでなければ私は答えを投稿しているだろうが、私はあなたができることがたくさんあることを疑う。 –

答えて

1

を表示します返されるアイテムの数を制限するAPIを提供します(たとえば、このプラクティスはXbox Marketplotエース)では、一部だけを表示することに決めたとしても、フィード全体をダウンロードします。

関連する問題