我正在使用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"));
app_id和app_code感到抱歉:P
在该代码中,我无法将数据显示到我的列表框中,但是当我使用它显示正确的数据时
MessageBox.Show(CurrentDay [3] .title.ToString());
哦,我正在使用mvvm light参考
我假设您的ListBox的“ItemsSource
属性”设置为CurrentDay
。
如果是这样,则ListBox需要知道该属性已更改。
由于您正在使用ObservableCollection
,所以ListBox会注意到当前集合的机会(例如Add或Remove Items
),但是ObservableCollection
除非您通知它,否则它不知道已向该属性分配了新属性。
为简单起见,建议您清除CurrentDay
集合并向其中添加项目,而不是new ObservableCollection<Item>()
每次都创建一个:
CurrentDay.Clear();
foreach (var item in result.results.items)
CurrentDay.Add(item);
确保在构造函数中初始化集合:
CurrentDay = new ObservableCollection<Item>();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句