我终于开始着手开发 Windows Phone 了。我还不是很擅长,但无论如何,我希望你们明白我想在这里做什么。
根据我从其他程序员那里学到的知识,ObservableCollection 可以在数据绑定到对象(例如列表框)时实时更新。对 ObservableCollection 的所有更改都将导致其数据绑定对象的 UI 更新其项目。
所以我想做的是从我的服务器下载一个文件,用 json 解析它,然后用新数据更新 ObservableCollection。但是,在重新打开应用程序之前,网络客户端似乎不会下载新数据!
这是一个 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
有人知道出了什么问题吗?:(
提前致谢。