【问题标题】:WP7 - Updating a listboxWP7 - 更新列表框
【发布时间】:2012-05-23 11:03:57
【问题描述】:

我终于开始着手开发 windows phone dev 了。我还不是很擅长,但无论如何,我希望你们明白我想在这里做什么。

根据我从其他程序员那里学到的知识,ObservableCollection 可以在数据绑定到对象(例如列表框)时实时更新。对 ObservableCollection 的所有更改都会导致其数据绑定对象的 UI 更新其项目。

所以我要做的是从我的服务器下载一个文件,用 json 解析它,然后用新数据更新 ObservableCollection。但是,在重新打开应用程序之前,webclient 似乎不会下载新数据!


这是一个 gif,展示了该应用目前的工作方式:

这是我的代码(稍微删减一下):

Dim aList As New ObservableCollection(Of classes.consoles)

Private Sub PhoneApplicationPage_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        checkforconsoles()
End Sub

Public Sub checkforconsoles()
        Dim wc As New WebClient()
        AddHandler wc.DownloadStringCompleted, AddressOf downloaded
        wc.DownloadStringAsync(New Uri("http://localhost/api/?function=GetConsolesForUser&userid=" & id))
    End Sub

    Private Sub downloaded(sender As Object, e As DownloadStringCompletedEventArgs)

        aList.Clear()
        'MessageBox.Show(e.Result)

        Dim o As JObject = JObject.Parse(e.Result)

        Dim jarray As JArray = DirectCast(o("results"), JArray)

        Try
            Dim i As Integer = jarray.Count()

            For i = 0 To jarray.Count() - 1 Step 1

                Dim id As String = jarray(i)("id").ToString
                Dim name As String = jarray(i)("name").ToString
                Dim image As String = jarray(i)("image").ToString

                MessageBox.Show(name)

                Dim c As classes.consoles = New classes.consoles()
                c.categoryimage = New Uri(image)
                c.categoryname = name
                c.categoryid = id

                aList.Add(c)
            Next

            listBoxview.ItemsSource = aList
            StackPanel1.Visibility = Windows.Visibility.Collapsed
            StackPanel2.Visibility = Windows.Visibility.Visible

        Catch ex As Exception
            StackPanel2.Visibility = Windows.Visibility.Collapsed
            StackPanel1.Visibility = Windows.Visibility.Visible
        End Try

    End Sub

Private Sub ApplicationBarIconButton_Click_1(sender As System.Object, e As System.EventArgs)
    checkforconsoles()
End Sub

有人知道出了什么问题吗? :(

提前致谢。

【问题讨论】:

  • 可能是缓存问题。试试这个:wc.DownloadStringAsync(New Uri("http://localhost/api/?function=GetConsolesForUser&userid=" & id & "&random=" + Guid.NewGuid().ToString()))
  • 显然是缓存问题
  • 添加新商品的代码是什么?您的 ApplicationBarIconButton_Click(我猜)事件
  • @keyboardP - 成功了!谢谢!您应该发表您的评论作为答案,以便我接受它:)
  • @JoelMurphy - 不客气。我已将其添加为答案。

标签: vb.net windows-phone-7 webclient observablecollection


【解决方案1】:

这是 WebClient 的缓存问题。您可以附加一个随机查询字符串以确保 URL 始终是唯一的,这样 WebClient 就不会缓存结果。一种方法是添加一个随机的 GUID 值,因为它不太可能在短时间内生成两个相同的 GUID。

wc.DownloadStringAsync(New Uri("http://localhost/api/?function=GetConsolesForUser&
                         userid=" & id & "&random=" + Guid.NewGuid().ToString()))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多