简体   繁体   中英

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. Whenever I call my RSS feed, I pull all the items upfront. 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.

Where in my code can I go about limiting how much of the RSS is shown at a time?

I push this button to load up the RSS feed which in turns displays the items. This is the C# code behind it:

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:

 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

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?

(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.

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.

Then once you click the button, you clear the observablecollection, and increase the offset, before inserting more from the given point.

Using Enumerable.Skip and Enumerable.Take makes this very easy. If you still haven't understood UI bindings, I recommend you read a tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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