【问题标题】:Windows phone refresh/update listbox itemsWindows phone 刷新/更新列表框项目
【发布时间】: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


【解决方案1】:

首先更新这个方法:

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

更新:

看起来您的请求已被操作系统缓存。您可以做的是在您的网址中添加一些随机文本。

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

【讨论】:

  • 成功了吗?如果没有,让我们尝试调试它。
  • 我将不得不稍等片刻才能看到它是否有效。因为它从 rss 获得最新消息,但自从我启动该应用程序以来就没有新的提要,但我会在知道它是否有效后立即发布:)。
  • 不,它没有用。问题仍然存在。当我第一次启动该应用程序时,它会显示所有提要。但是,当我获取提要的 RSS 得到更新时,我按下刷新按钮,它仍然只显示相同的提要。
  • 好的,然后给webClient_DownloadStringCompleted方法下一个断点,检查它是否被调用,是否在e.Result中得到更新的xml
  • 好的,我测试过了。 webClient_DownloadStringCompleted 方法确实被调用,但 e.Result 中的 xml 没有更新,它仍然使用旧数据,出于某种原因。
猜你喜欢
  • 2014-04-10
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多