简体   繁体   中英

windows phone binding data

I'm working with windows phone 8 apps that getting data from restful service using json file and I getting problem to showing data I get from restful service into my listbox here is the code:

  WebClient client = new WebClient(); client.DownloadStringCompleted += (s,e) => { if(e.Error == null){ RootObject result = JsonConvert.DeserializeObject<RootObject>(e.Result); CurrentDay = new ObservableCollection<Item>(result.results.items); MessageBox.Show(CurrentDay[3].title.ToString()); }else{ MessageBox.Show("Sorry, try again at the next TechEd"); } }; client.DownloadStringAsync(new Uri("http://places.nlp.nokia.com/places/v1/discover/explore?at=37.7851%2C-122.4047&cat=transport&tf=plain&pretty=true&app_id=xxxxx&app_code=xxxxx")); 

the app_id and app_code sensored sorry :P

in that code i can't show the data into my listbox but when i using it showing the right data

MessageBox.Show(CurrentDay[3].title.ToString());

oh and I'm using mvvm light reference

I assume your ListBox has ItemsSource Property set to CurrentDay .

If that is true, the ListBox needs to know that the property was changed.

Since you are using ObservableCollection , the ListBox will notice chances to the current collection (eg Add or Remove Items ), but it can't know that a new ObservableCollection is assigned to the property unless you notify it.

To make it simple, I suggest you to Clear the CurrentDay collection and Add items to it, instead of creating a new ObservableCollection<Item>() each time:

CurrentDay.Clear();
foreach (var item in result.results.items)
    CurrentDay.Add(item);

Make sure to initialize the collection in the constructor:

CurrentDay = new ObservableCollection<Item>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM