vb.net - WP7 - 更新列表框

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

我终于开始启动 Windows Phone 开发了。我还不太擅长,但无论如何,我希望你们明白我想在这里做什么。

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

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

<小时/>

下面的 gif 展示了该应用目前的工作原理: enter image description here

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

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

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

提前致谢。

最佳答案

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

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

关于vb.net - WP7 - 更新列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10609143/

相关文章:

c# - .NET ToolStrip 标准图标的位置

vb.net - VShost32.exe 停止工作,但我可以继续调试

asp.net - VB.Net 路径中的反斜杠

visual-studio-2010 - Windows Phone 7.1 - 如何在按下按钮时更改背景图像?

c# - 需要帮助在 C# 中读取 xml 文件

c# - 将 WebClient 错误作为字符串获取

vb.net - 文件系统 FilePutobject 问题

c# - 如何离线存储密码

c# - Web 客户端 - 在此上下文中不允许异步操作

vb.net - 如何让WebClient使用Cookies?