绑定到列表框时从列表框中删除第一个项目

时间:2014-10-25 07:20:08

标签: windows-phone-8

我已经解析了Feed http://www.toucheradio.com/toneradio/android/toriLite/AndroidRSS.xml 并且所有来自rss feed的项目都显示在listbox中。现在我不想得到第一项我必须省略它。我怎么能这样做。

MainPage.xaml.cs中:

namespace tori
{
    public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();


        // is there network connection available
       if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
        {
            MessageBox.Show("No network connection available!");
            return;
        }
        // start loading XML-data
        WebClient downloader = new WebClient();
        Uri uri = new Uri(" http://www.toucheradio.com/toneradio/android/toriLite/AndroidRSS.xml", UriKind.Absolute);
        downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ChannelDownloaded);
        downloader.DownloadStringAsync(uri);
    }

    void ChannelDownloaded(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result == null || e.Error != null)
        {
            MessageBox.Show("There was an error downloading the XML-file!");
        }
        else
        {
            // Deserialize if download succeeds

            XDocument document = XDocument.Parse(e.Result);

            var queue = from item in document.Descendants("item")
                        select new Item
                        {
                            title = item.Element("title").Value
                            ,
                            link = item.Element("link").Value
                            ,
                            ThumbnailUrl = item.Element(item.GetNamespaceOfPrefix("media") + "thumbnail").Attribute("url").Value
                            ,

                        };

           itemsList.ItemsSource = queue;
}
}

 public class Item
       {
             public string title { get; set; }
            public string ThumbnailUrl { get; set; }
            public string link { get; set; }
       }

如果我写queue.RemoveAt(0); 我在RemoveAt遇到错误。 任何人都可以告诉我我该怎么做。 非常感谢提前。

1 个答案:

答案 0 :(得分:0)

您可以使用LINQ Skip扩展方法(文档here)轻松完成此操作。 试试下面的示例:

itemsList.ItemsSource = queue.Skip(1);

同样,您可以使用方法链方法在select方法中应用投影之前重写查询以省略第一项:

var queue = document.Descendants("item")
            .Skip(1)
            .Select(item => new Item
                    {
                        title = item.Element("title").Value,
                        link = item.Element("link").Value,
                        ThumbnailUrl = item.Element(item.GetNamespaceOfPrefix("media") + "thumbnail").Attribute("url").Value,
                    })
            .ToList();

itemsList.ItemsSource = queue;

更新/由于评论

如果您需要跳过某些索引中的项目,您可以使用Where方法和HasSet,如下例所示:

var document = XDocument.Parse(e.Result);
var indexes = new HashSet<int> { 1, 3, 4 }; 

var queue = document.Descendants("item")
    .Select(item => new Item
    {
        title = item.Element("title").Value,
        link = item.Element("link").Value,
        ThumbnailUrl =
            item.Element(item.GetNamespaceOfPrefix("media") + "thumbnail").Attribute("url").Value,
    })
    .Where((x, i) => !indexes.Contains(i))
    .ToList();

Items.ItemsSource = queue;