【发布时间】:2014-03-10 08:49:50
【问题描述】:
我的 RSS 提要应用程序有问题。当我的应用程序启动时,列表框获取提要并将它们显示在我的列表框中,但是当我按下刷新按钮时,列表框永远不会更新,它只会再次显示相同的项目,但如果我关闭应用程序,然后重新启动它,它将显示最新的提要。我希望有人可以提供帮助。谢谢。
MainWindow.xaml:
<ListBox Grid.Row="1" Name="feedListBox" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="feedListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
...
...
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
MainWindow.cs:
private void appBarRefresh_Click(object sender, EventArgs e)
{
feedListBox.ItemsSource = null;
GetFeed(IsolatedStorageSettings.ApplicationSettings["key"].ToString());
}
private void GetFeed(string rss)
{
WebClient webClient = new WebClient();
webClient.DownloadStringAsync(new System.Uri(rss));
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
}
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(e.Error.Message);
});
}
else
{
this.State["feed"] = null;
this.State.Clear();
this.State["feed"] = e.Result;
UpdateFeedList(e.Result);
}
}
private void UpdateFeedList(string feedXML)
{
StringReader stringReader = new StringReader(feedXML);
XmlReader xmlReader = XmlReader.Create(stringReader);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
feedListBox.ItemsSource = feed.Items;
});
;
}
来自 cmets 的 UPD:
在我的 OnNavigatedTo 方法中,我运行以下代码:
if (this.State.ContainsKey("feed"))
{
if (feedListBox.Items.Count == 0)
{
UpdateFeedList(State["feed"] as string);
}
}
【问题讨论】:
-
第一次打开应用时如何填写订阅列表?
-
在我的 OnNavigatedTo 方法中,我运行以下代码: if (this.State.ContainsKey("feed")) { if (feedListBox.Items.Count == 0) { UpdateFeedList(State["feed" ] 作为字符串); } }
标签: c# listbox rss windows-phone