WP7 ArrayAdapter 等效项
我有一个从 WCF 服务返回的 JSON 对象,它返回两个“Article”对象,如下所示:
{"GetArticlesResult":[{"ArticleName":"Mobile Application Development","ArticleText":"Lots of text here."},{"ArticleName":"Super Fast Development Cycle","ArticleText":"Lots more text here."}]}
我可以使用以下代码将其显示在消息框中:
MessageBox.Show(e.Result);
我想要做的是在 ListBox 中显示 ArticleNames 列表,然后我想将它们链接到另一个页面以显示 ArticleText,但现在这是稍后的事情。
我的研究表明我需要使用 DataContractJsonSerializer 但这就是我陷入困境的地方,所有示例似乎都有点复杂。在 Android 应用程序中,我只需使用 ArrayAdapter 来填充一些内容。我可以在 WP7 应用程序中执行类似的操作吗?
干杯,
迈克。
编辑
我现在有以下内容,如何在列表框中显示结果?
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(e.Result));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Article));
//put ms into a listBox here????....
[DataContract]
public class Article
{
[DataMember()]
public string Title { get; set; }
[DataMember()]
public string Text { get; set; }
}
I have a JSON object returned from a WCF Service, it returns two “Article” objects and looks like this:
{"GetArticlesResult":[{"ArticleName":"Mobile Application Development","ArticleText":"Lots of text here."},{"ArticleName":"Super Fast Development Cycle","ArticleText":"Lots more text here."}]}
And I can display it in a message box with this code:
MessageBox.Show(e.Result);
What I want to do is display a list of ArticleNames in a ListBox, I will then want to link them to another page to display the ArticleText but that’s later for now.
My research has shown me that I need to use the DataContractJsonSerializer but this is where I’m stuck, all the examples seem to be a little complicated. In and Android app I would simply use an ArrayAdapter to populate something. Can I do something similar in a WP7 app?
Cheers,
Mike.
EDIT
I now have the following, how do I display the results in a ListBox?
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(e.Result));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Article));
//put ms into a listBox here????....
[DataContract]
public class Article
{
[DataMember()]
public string Title { get; set; }
[DataMember()]
public string Text { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将列表框的
ItemsSource
属性设置为数组/列表(或ObservableCollection
如果您要在显示时添加更多项目)。如果您使用的是 ViewModel,则可以将 ItemsSource 属性绑定到 VM 上的 ObservableCollection 属性,它将自动更新。您可以使用 JSON.NET (它比
DataContractJsonSerializer
更快)来反序列化 JSON到强类型对象模型并绑定它,或者您可以使用相同的库将 JSON 反序列化为动态,然后从中访问 JSON 属性。如果您使用强类型对象模型,则需要实现 xyzzer 对 Article/ArticleList 所做的更改,以便其正确映射。
You need to set the
ItemsSource
property of the listbox to an array/list (orObservableCollection
if you're going to add more items while it's displaying). If you are using a ViewModel, you can bind the ItemsSource property to an ObservableCollection property on your VM and it will update automatically.You can use JSON.NET (it's faster than
DataContractJsonSerializer
) to either deserialize the JSON to a strongly typed object model and bind that or you can use the same library to deserialize the JSON to adynamic
and then access the JSON properties from that.If you use a strongly typed object model, you'll need to implement the changes xyzzer made to Article/ArticleList in order for it to map properly.
也许列表可以帮助您从 JSON 结构反序列化/表示数组。否则 - 这个问题太模糊了。您是想问如何在 ListBox 中显示集合?使用 ItemsSource 绑定...
*编辑
试试这个:
Maybe a List would work for you to deserialize/represent an array from a JSON structure. Otherwise - the question is too vague. Are you asking how to display a collection in a ListBox? Using an ItemsSource binding...
*EDIT
Try this: