Windows Phone刷新/更新列表框项目 [英] Windows phone refresh/update listbox items

查看:74
本文介绍了Windows Phone刷新/更新列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对RSS feed应用有问题.当我的应用程序启动时,列表框会获取供稿,并在我的列表框中显示它们,但是当我按下刷新"按钮时,列表框将永远不会更新,它只会再次显示相同的项目,但是如果我关闭该应用程序,然后重新启动它,它将将显示最新的提要.我希望有人可以提供帮助.谢谢.

I have a problem with a RSS feed app. When my app launches, the listbox gets the feeds and show them in my listbox, but when i press my refresh button the listbox never updates, it just show the same items again, but if i close the app, and then relaunch it, it will show the latest feeds. I hope there is someone that can help. Thanks.

MainWindow.xaml:

MainWindow.xaml:

<ListBox Grid.Row="1" Name="feedListBox" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="feedListBox_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        ...
                        ...
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

MainWindow.cs:

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方法中,运行以下代码:

In my OnNavigatedTo method I run this code:

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

推荐答案

首先更新此方法:

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缓存.您可以做的就是在您的网址中添加一些随机文本.

Looks like your request was cached by OS. What you can do to is to add some random text to your url.

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

这篇关于Windows Phone刷新/更新列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆