如何将信息从xml文件插入列表框? [英] How do you insert information from an xml file into a listbox?

查看:91
本文介绍了如何将信息从xml文件插入列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我所拥有的。我希望在用户选择一个术语后,xml文件会出现在列表框中。然而,屏幕只是空白。有什么想法吗?



Here is what I have. I would expect the xml file to appear in the listbox after the user selects a term. However the screen just goes blank. Any ideas?

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;

namespace PhoneApp5
{
    public partial class MainPage : PhoneApplicationPage
    {
        public class Item
        {
            public string ItemLine1 { get; set; }
            public string ItemLine2 { get; set; }
        }

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            
            PageTitle.Text = "Terms";
            List<Item> list = new List<Item>();
            Item item = new Item();
            item.ItemLine1 = "Third Summer 2013";
            item.ItemLine2 = "Classes";
            list.Add(item);
            item = new Item();
            item.ItemLine1 = "Second Summer 2013";
            item.ItemLine2 = "Classes";
            list.Add(item);
            item = new Item();
            item.ItemLine1 = "First Summer 2013";
            item.ItemLine2 = "Classes";
            list.Add(item);
            item = new Item();
            item.ItemLine1 = "Spring 2013";
            item.ItemLine2 = "classes";
            list.Add(item);
            item = new Item();
            item.ItemLine1 = "Fall 2012";
            item.ItemLine2 = "Classes";
            list.Add(item);
            
            Dispatcher.BeginInvoke(new Action(() => ListBox1.ItemsSource = list));

        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
           ApplicationTitle.Text = e.Result;
        }

        private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if (sender != null) PageTitle.Text = sender.ToString();
            //if (e != null) PageTitle.Text = e.AddedItems.Count.ToString();

            //IEnumerator ie = e.AddedItems.GetEnumerator();
            //ie.MoveNext();
            //if (e != null) ApplicationTitle.Text = ie.Current.ToString();

             WebClient wc = new WebClient();
             wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
             wc.DownloadStringAsync(new Uri("http://www.usi.edu/webservices/iphone/USIINFO201310.xml"));
             
             
        }
    }

推荐答案

您是否尝试过调试应用程序?



在你的情况下,你在MainPage构造函数的末尾填充你最终调用ListBox1_SelectionChanged方法的列表框,并且屏幕空白的原因可能是因为操作执行side SelectionChanged方法。



仔细检查wc.DownloadStringAsync不会使你的页面空白。



值得注意的是
have you tried debugging your application ?

In your case at the end of MainPage constructor you are filling your list box which eventually makes call to ListBox1_SelectionChanged method and the reason for the screen blank may be because of the operation performed side SelectionChanged method.

double check wc.DownloadStringAsync doesnt blank your page.

it''s worth to note that
ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 

方法。


这篇关于如何将信息从xml文件插入列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆