2012-05-15 73 views
3

我終於開始了Windows Phone開發。我還不是很擅長,但無論如何,我希望你們明白我想在這裏做什麼。WP7 - 更新列表框

從我從其他程序員那裏學到的知識,ObservableCollection可以在現場時間更新,而它是綁定到一個對象(如列表框)的數據。對ObservableCollection的所有更改都會導致其數據綁定對象的UI更新其項目。

所以我試圖做的是從我的服務器下載一個文件,用json解析它,然後用新數據更新ObservableCollection。然而,該webclient似乎並沒有下載新的數據,直到應用程序重新打開!


這裏顯示的應用程序是如何工作的,此刻的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 

沒有任何人有任何線索,什麼是錯的? :(

在此先感謝。

+3

這可能是緩存的問題試試這個:'wc.DownloadStringAsync(新的URI( 「http:// localhost/api /?function = GetConsolesForUser&userid =」&id&「&random =」+ Guid.NewGuid()。ToString()))' – keyboardP

+1

絕對看起來像是一個緩存問題 –

+0

添加新項目的代碼是什麼?你的ApplicationBarIconButton_Click(我猜)事件 –

回答

2

這是一個cachine問題與WebClient的。您可以附加一個隨機查詢字符串,以確保URL始終是唯一的,這樣Web客戶端不緩存的結果。一種方法要做到這一點是添加一個隨機的GUID值,因爲它是不太可能產生兩個相同的GUID在很短的時間內

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