简体   繁体   English

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

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

The app I am working on is at a standstill and I've been wracking my brain for the past few days trying to figure out how to get this to be implemented. 我正在开发的应用程序处于停滞状态,过去几天来我一直在绞尽脑汁,试图弄清楚如何实现这一目标。

My problem is is that I am trying to implement a virtualized data set into my RSS xml feed. 我的问题是我正在尝试在RSS xml提要中实现虚拟化数据集。 Whenever I call my RSS feed, I pull all the items upfront. 每当我调用RSS提要时,我都会预先提取所有项目。 However, I only want it to pull, say, 15 at a time, with the user having the option to push a button to load more and group them. 但是,我只希望它一次拉15个,而用户可以选择按下按钮来加载更多内容并进行分组。

Where in my code can I go about limiting how much of the RSS is shown at a time? 我可以在代码的哪个位置限制一次显示多少RSS?

I push this button to load up the RSS feed which in turns displays the items. 我按此按钮加载RSS提要,该提要又显示项目。 This is the C# code behind it: 这是其背后的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
            );
    }
   }
  }

Here is my RSS service class: 这是我的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));
    }
   }
  }

and finally the RSSItems Class 最后是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; }
   }
}

I have the links which sort of show how to do this, but I just cannot get this to work on my own. 我有一些链接显示了如何执行此操作,但是我无法单独使用它。 How on earth am I able to implement code to figure out how many RSS items I have in the feed, and then to limit it to an X amount, with the ability to add on more in the phone? 我到底能实现代码以弄清供稿中有多少RSS项目,然后将其限制为X数量,并且能够在电话中添加更多内容?

(I want to do exactly like this example does: http://shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight-for-Windows-Phone-7-Data-Virtualization.aspx ) but with the button to add more on. (我想做的例子与此完全一样: http : //shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight-for-Windows-Phone-7-Data-Virtualization.aspx ),但带有按钮以添加更多内容。

As you already been told, you should bind to a observablecollection with your items in. If you really want to force the user to click a button to load more (silly approach really, and terrible UX), then you need to keep a internal collection with all items in, and a offset (defaulting to zero), besides the UI bound observablecollection. 正如您已经被告知的那样,您应该将项目绑定到一个observablecollection中。如果您确实想强迫用户单击一个按钮来加载更多内容(确实是愚蠢的方法,并且糟糕的UX),那么您需要保留一个内部集合除了UI绑定的observablecollection外,所有项都包含在内,并且有一个偏移量(默认为零)。

Then once you click the button, you clear the observablecollection, and increase the offset, before inserting more from the given point. 然后,一旦单击按钮,就清除observablecollection,并增加偏移量,然后再从给定点插入更多内容。

Using Enumerable.Skip and Enumerable.Take makes this very easy. 使用Enumerable.SkipEnumerable.Take使其非常容易。 If you still haven't understood UI bindings, I recommend you read a tutorial . 如果您仍然不了解UI绑定,建议您阅读教程

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

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