2012-01-12 28 views
0

我對Windows Mobile Development(.net框架也是)相當陌生,我一直在試圖找出解決這個問題的方法,一段時間:我使用WebClient獲取一些數據後沒有更新數據綁定

1 - 我有一個視圖模型,實現INotifyPropertyChanged的包含我的人名單:

public ObservableCollection<Person> listOfPeople; 

2 - 我綁定listOfPeople到列表框;

3 - 我在MainPage.xaml.cs中創建了一些虛擬數據,它更新了列表框。

4 - 問題是當我嘗試從我下載的rss更新人員信息時:對於每個人對象,我調用其Person.updateData()方法。該方法從rss訂閱源獲取一些信息:

internal void updateData() 
     { 
      WebClient wc = new WebClient(); 
      wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); 
      wc.DownloadStringAsync(new Uri(sourceUrl)); 

     } 


     void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 
      //Parse the data: 
       ... 

      //Try to update: 

       updateAge(age); 
       updateAddress(Address);     
      } 

     } 

而且值在類中更新,但在UI中沒有更新。 我搜索後發現處理程序在diff線程中運行,這可能是問題所在。 我試過

Deployment.Current.Dispatcher.BeginInvoke(delegate { updateSeason(season); }); 

但沒有工作。

任何人都可以幫助我嗎?

謝謝

回答

0

另一個線程可能不是一個原因,因爲DownloadStringCompleted事件處理程序在其WebClient創建的線程上運行(在這種情況下可能UI線程)。

這可能是太明顯的問題,但是當您嘗試更新UI時,您是否在ViewModel中生成了INotifyPropertyChanged.PropertyChanged事件?

+0

嗨,thx的幫助,但是我已經做到了。 當我調用更改除wc_DownloadStringCompleted內部以外的任何值時,UI將更新。我已經測試了它在模型查看器中,主類中,在自己的Person類中的值,並且它可以工作。只有當我嘗試在wc_DownloadStringCompleted中執行它時,它不會更新UI。 – FC777 2012-01-14 12:57:21

+0

@ user1146559 - 您需要展示更多代碼。 – Pol 2012-01-14 13:24:03