我的RSS Feed应用有问题。当我的应用程序启动时,列表框会获取源并在我的列表框中显示它们,但是当我按下我的刷新按钮时列表框永远不会更新,它只会再次显示相同的项目,但如果我关闭应用程序,然后重新启动它,它将显示最新的Feed。我希望有人能提供帮助。感谢。
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;
});
;
}
答案 0 :(得分:1)
在UpdateFeedList(string feedXML)
feedListBox.ItemsSource = null;
feedListBox.ItemsSource = feed.Items;
答案 1 :(得分:0)
您的列表是否正在实施INotifyCollectionChanged?如果它不是ObservableCollection,那么对其内容的更改将不会自动反映在UI中。
以下thread
的更多信息