通过RSS XML项构建可观察集合

本文关键字:观察 集合 构建 RSS XML 通过 | 更新日期: 2023-09-27 18:03:15

简单地说,我有我的RSSItem类和我的RSSService类,和一个按钮点击来加载它们。我如何将它们加载到一个可观察集合中,以便我可以操纵数据?

命名空间和加载代码的按钮:

public partial class UserSubmitted : PhoneApplicationPage
{
    private const string WindowsPhoneBlogPosts = "http://www.example/feed.xml";
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        RssService.GetRssItems(
            WindowsPhoneBlogPosts,
            (items) => { listbox.ItemsSource = items; },
            (exception) => { MessageBox.Show(exception.Message); },
            null
            );
    }
}

rss项目代码:

namespace WindowsPhone.Helpers
{
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, "<[^>]+?>", ""));
    }
    /// <summary>
    /// Gets or sets the title.
    /// </summary>
    /// <value>The title.</value>
    public string Title { get; set; }
    /// <summary>
    /// Gets or sets the summary.
    /// </summary>
    /// <value>The summary.</value>
    public string Summary { get; set; }
    /// <summary>
    /// Gets or sets the published date.
    /// </summary>
    /// <value>The published date.</value>
    public string PublishedDate { get; set; }
    /// <summary>
    /// Gets or sets the URL.
    /// </summary>
    /// <value>The URL.</value>
    public string Url { get; set; }
    /// <summary>
    /// Gets or sets the plain summary.
    /// </summary>
    /// <value>The plain summary.</value>
    public string PlainSummary { get; set; }
   }
 }
服务代码:

namespace WindowsPhone.Helpers
{ 
public static 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<IEnumerable<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
                List<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));
    }
  }
}

通过RSS XML项构建可观察集合

您可以使用'new'关键字创建一个ObservableCollection,如下所示:

ObservableCollection<RssItem> rssItems = new ObservableCollection<RssItem>();

您可以通过Add方法添加它们:

RssItem rssItem = new RssItem( /* initializationcode here */ );
rssItems.Add(rssItem);