gpt4 book ai didi

vb.net - WP7 - 更新列表框

转载 作者:行者123 更新时间:2023-12-02 05:41:12 25 4
gpt4 key购买 nike

我终于开始启动 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com