Windows手机刷新/更新列表框项目

时间:2014-03-10 08:49:51

标签: c# listbox rss windows-phone

我的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;
    });
    ;
}

评论UPD:

在我的OnNavigatedTo方法中,我运行此代码:

if (this.State.ContainsKey("feed")) 
{ 
    if (feedListBox.Items.Count == 0) 
    { 
        UpdateFeedList(State["feed"] as string); 
    } 
}

1 个答案:

答案 0 :(得分:0)

首先更新此方法:

private void GetFeed(string rss)
{
    //register event handler first, then call the async method
    WebClient webClient = new WebClient();
    webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
    webClient.DownloadStringAsync(new System.Uri(rss)); 
}

<强>更新

您的请求看起来像是由OS缓存的。您可以做的是在您的网址中添加一些随机文字。

webClient.DownloadStringAsync(new System.Uri(rss + "?disablecache="+Environment.TickCount));