简体   繁体   English

Windows Phone绑定数据

[英]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: 我正在使用Windows Phone 8应用程序,这些应用程序使用json文件从静态服务获取数据,但我无法将从静态服务获取的数据显示到列表框中,这是代码:

  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 app_id和app_code感到抱歉: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()); MessageBox.Show(CurrentDay [3] .title.ToString());

oh and I'm using mvvm light reference 哦,我正在使用mvvm light参考

I assume your ListBox has ItemsSource Property set to CurrentDay . 我假设您的ListBox的ItemsSource属性设置为CurrentDay

If that is true, the ListBox needs to know that the property was changed. 如果是这样,则ListBox需要知道该属性已更改。

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. 由于您使用的是ObservableCollection ,因此ListBox会注意到当前集合的机会(例如Add或Remove Items ),但是除非您通知它,否则它不知道ObservableCollection该属性分配了新的ObservableCollection

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 ,建议您清除CurrentDay集合并向其中添加项目,而不是每次都创建一个new ObservableCollection<Item>()

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>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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