繁体   English   中英

桥接两段代码时遇到麻烦,Windows Phone 7

[英]Having trouble bridging two pieces of code, Windows Phone 7

我正在开发的应用程序处于停滞状态,过去几天来我一直在绞尽脑汁,试图弄清楚如何实现这一目标。

我的问题是我正在尝试在RSS xml提要中实现虚拟化数据集。 每当我调用RSS提要时,我都会预先提取所有项目。 但是,我只希望它一次拉15个,而用户可以选择按下按钮来加载更多内容并进行分组。

我可以在代码的哪个位置限制一次显示多少RSS?

我按此按钮加载RSS提要,该提要又显示项目。 这是其背后的C#代码:

namespace App
{
public partial class UserSubmitted : PhoneApplicationPage
{
    private const string MyFeed = "http://www.myfeed.com/feed.xml";


    private void CallRssandDisplay_Click(object sender, RoutedEventArgs e)
    {
        RssService.GetRssItems(
            MyFeed,
            (items) => { listbox.ItemsSource = items; },
            (exception) => { MessageBox.Show(exception.Message); },
            null
            );
    }
   }
  }

这是我的RSS服务类别:

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Net;
 using System.ServiceModel.Syndication;
 using System.Xml;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Threading;
 using System.Windows;
 using System.Windows.Input;
 using MyFeed.Helpers;


 namespace MyFeed.Helpers
 { 

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

最后是RSSItems类

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace MyFeed.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; }
   }
}

我有一些链接显示了如何执行此操作,但是我无法单独使用它。 我到底能实现代码以弄清供稿中有多少RSS项目,然后将其限制为X数量,并且能够在电话中添加更多内容?

(我想做的例子与此完全一样: http : //shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight-for-Windows-Phone-7-Data-Virtualization.aspx ),但带有按钮以添加更多内容。

正如您已经被告知的那样,您应该将项目绑定到一个observablecollection中。如果您确实想强迫用户单击一个按钮来加载更多内容(确实是愚蠢的方法,并且糟糕的UX),那么您需要保留一个内部集合除了UI绑定的observablecollection外,所有项都包含在内,并且有一个偏移量(默认为零)。

然后,一旦单击按钮,就清除observablecollection,并增加偏移量,然后再从给定点插入更多内容。

使用Enumerable.SkipEnumerable.Take使其非常容易。 如果您仍然不了解UI绑定,建议您阅读教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM