3

私はついにWindowsPhone開発を開始することになりました。私はまだあまり得意ではありませんが、とにかく、皆さんが私がここでやりたいことを理解してくれることを願っています。

私が他のプログラマーから学んだことから、ObservableCollectionは、リストボックスなどのオブジェクトにデータバインドされている間に、ライブタイムで更新できます。ObservableCollectionにすべての変更を加えると、データバインドされたオブジェクトのUIがアイテムを更新します。

つまり、私がやろうとしているのは、サーバーからファイルをダウンロードし、それをjsonで解析してから、ObservableCollectionを新しいデータで更新することです。ただし、アプリが再度開かれるまで、Webクライアントは新しいデータをダウンロードしていないようです。


これは、現時点でアプリがどのように機能するかを示す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

誰かが何が悪いのか手がかりを持っていますか?:(

前もって感謝します。

4

1 に答える 1

2

これは、WebClientの問題です。ランダムなクエリ文字列を追加して、WebClientが結果をキャッシュしないように、URLが常に一意になるようにすることができます。これを行う1つの方法は、ランダムなGUID値を追加することです。これは、短い時間枠で同じGUIDが2つ生成される可能性が非常に低いためです。

wc.DownloadStringAsync(New Uri("http://localhost/api/?function=GetConsolesForUser&
                         userid=" & id & "&random=" + Guid.NewGuid().ToString()))
于 2012-05-16T11:42:59.137 に答える