如何将信息从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)
方法。