簡體   English   中英

Windows Phone刷新/更新列表框項目

[英]Windows phone refresh/update listbox items

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

首先更新此方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM