c# - 将 XML 项动态添加到列表框时出现问题,C#?

标签 c# silverlight windows-phone-7 dynamic

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

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

我在哪里可以将代码注入(inject)到这个示例中以允许动态加载项目,比如一次加载 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# 绑定(bind)到页面以显示提要

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

最佳答案

除非托管提要的服务器提供 API 来限制退回元素的数量(例如,这种做法用于 Xbox 商城),否则您将下载整个提要,即使您决定只显示一部分。

关于c# - 将 XML 项动态添加到列表框时出现问题,C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6893370/

相关文章:

c# - Stop OnClientClick ="aspnetForm.target =' _blank';"从其他控件点击中触发?

c# - 让 Awesomium 在 linux 下运行在 mono 上

c# - 最小 Silverlight 示例

silverlight - 使用什么控件在 Windows Phone 7 上显示更大的图像

silverlight - 为什么 WP7 ListPicker 的边距和高度与 TextBox 不同

c# - 如何比较 Office 互操作对象是否相等?

c# - 如何根据登录类型从母版页中隐藏特定菜单项?

silverlight - 更改 windows phone 7 silverlight 项目命名空间导致启动失败

c# - 元素 TextBlock WP 7 上的未知属性前景

c# - 如何比较两个捕获的声音,看看哪一个声音更大?