簡體   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