我的列表框没有更新/刷新时遇到问题。我在这里读过我需要使用ObservableCollection
但我没有任何运气。我正在从 XML 填充我的列表框。
public class PestotoraPost
{
public string ID { get; set; }
public string Date { get; set; }
public string Name { get; set; }
public string Message { get; set; }
}
void WebLoad()
{
WebClient pestotora = new WebClient();
pestotora.DownloadStringCompleted += new DownloadStringCompletedEventHandler(pestotora_DownloadStringCompleted);
pestotora.DownloadStringAsync(new Uri("wwww.someURL.com/xml.php"));
}
请注意,实际的 xml 是包含 xml 结构的 php(来自 sql DB)
void pestotora_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XElement doc = XElement.Parse(e.Result);
listBox1.ItemsSource = from results in doc.Descendants("Data")
select new PestotoraPost
{
ID = results.Element("DataID").Value,
Date = results.Element("DataDate").Value,
Name=results.Element("DataName").Value,
Message=results.Element("DataMessage").Value
};
}
每次 XML 更改时,我的列表框都不会更新listBox1.UpdateLayout();
关于如何开始实施这个的任何线索/帮助?非常感谢。
更新
namespace pestotora
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
UIload(true);
WebLoad();
}
public ObservableCollection<PestotoraPost> Posts { get; set; }
public class PestotoraPost
{
public string ID { get; set; }
public string Date { get; set; }
public string Name { get; set; }
public string Message { get; set; }
}
void WebLoad()
{
WebClient pestotora = new WebClient();
pestotora.DownloadStringCompleted += new DownloadStringCompletedEventHandler(pestotora_DownloadStringCompleted);
pestotora.DownloadStringAsync(new Uri("www.domain.com/xml.php"));
}
void UIload(bool Splash)
{
if (Splash == true)
{
ApplicationBar.IsVisible = false;
}
else
{
ApplicationBar.IsVisible = true;
}
}
void pestotora_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
//XDocument doc = XDocument.Parse(e.Result);
XElement doc = XElement.Parse(e.Result);
/* listBox1.ItemsSource = from results in doc.Descendants("Data")
select new PestotoraPost
{
ID = results.Element("DataID").Value,
Date = results.Element("DataDate").Value,
Name=results.Element("DataName").Value,
Message=results.Element("DataMessage").Value
}; */
var list = from results in doc.Descendants("Data")
select new PestotoraPost
{
ID = results.Element("DataID").Value,
Date = results.Element("DataDate").Value,
Name = results.Element("DataName").Value,
Message = results.Element("DataMessage").Value
};
Posts = new ObservableCollection<PestotoraPost>(list);
foreach (var post in list)
{
Posts.Add(post);
}
UIload(false);
}
private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
{
//stckPost.Visibility = System.Windows.Visibility.Visible;
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/Post.xaml", UriKind.Relative));
}
private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
listBox1.UpdateLayout();
//listBox1.ScrollIntoView(listBox1.Items[0]);
WebLoad();
}
}
}