将XML项动态添加到列表框C#的问题?

时间:2011-08-01 01:00:45

标签: c# silverlight windows-phone-7 dynamic

我正在为我的RSS项目提取Feed,而我遇到了一个不知道如何允许用户将更多项目加载到集合中的问题。目前,一切都在加载。虽然这在某些情况下是正确的,但我希望用户能够在移动连接速度较慢的情况下选择加载方式。

这是借用的代码,因此只会增加我的困惑。

我在哪里可以将代码注入此示例以允许动态加载项目,例如,一次30个?

Rss课程:

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

1 个答案:

答案 0 :(得分:1)

除非托管订阅源的服务器提供API以限制返回的项目数(例如,此练习用于Xbox Marketplace),即使您决定仅显示,也将下载整个订阅源它的一部分。