WP7 ArrayAdapter 等效项

发布于 2024-12-16 21:41:20 字数 1052 浏览 0 评论 0原文

我有一个从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脸赞 2024-12-23 21:41:20

您需要将列表框的 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 (or ObservableCollection 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 a dynamic 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.

绝不放开 2024-12-23 21:41:20

也许列表可以帮助您从 JSON 结构反序列化/表示数组。否则 - 这个问题太模糊了。您是想问如何在 ListBox 中显示集合?使用 ItemsSource 绑定...

*编辑

试试这个:

var jsonString = "{\"GetArticlesResult\":[{\"ArticleName\":\"Mobile Application Development\",\"ArticleText\":\"Lots of text here.\"},{\"ArticleName\":\"Super Fast Development Cycle\",\"ArticleText\":\"Lots more text here.\"}]}";
var articles = Deserialize<ArticleList>(jsonString);
Debug.WriteLine(articles.GetArticlesResult.Count);

[DataContract]
public class ArticleList
{
    [DataMember]
    public List<Article> GetArticlesResult { get; set; }
}

[DataContract]
public class Article
{
    [DataMember(Name = "ArticleName")]
    public string ArticleName { get; set; }

    [DataMember(Name = "ArticleText")]
    public string ArticleText { get; set; }
}

public static T Deserialize<T>(string strData) where T : class
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
    byte[] byteArray = Encoding.UTF8.GetBytes(strData);
    MemoryStream memoryStream = new MemoryStream(byteArray);
    T tRet = serializer.ReadObject(memoryStream) as T;
    memoryStream.Dispose();
    return tRet;
}

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:

var jsonString = "{\"GetArticlesResult\":[{\"ArticleName\":\"Mobile Application Development\",\"ArticleText\":\"Lots of text here.\"},{\"ArticleName\":\"Super Fast Development Cycle\",\"ArticleText\":\"Lots more text here.\"}]}";
var articles = Deserialize<ArticleList>(jsonString);
Debug.WriteLine(articles.GetArticlesResult.Count);

[DataContract]
public class ArticleList
{
    [DataMember]
    public List<Article> GetArticlesResult { get; set; }
}

[DataContract]
public class Article
{
    [DataMember(Name = "ArticleName")]
    public string ArticleName { get; set; }

    [DataMember(Name = "ArticleText")]
    public string ArticleText { get; set; }
}

public static T Deserialize<T>(string strData) where T : class
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
    byte[] byteArray = Encoding.UTF8.GetBytes(strData);
    MemoryStream memoryStream = new MemoryStream(byteArray);
    T tRet = serializer.ReadObject(memoryStream) as T;
    memoryStream.Dispose();
    return tRet;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文