如何通过Web API(在线服务)绑定JSON数据与xaml UI在Windows应用商店

本文关键字:UI xaml Windows 应用 数据 JSON API Web 何通过 在线服务 绑定 | 更新日期: 2023-09-27 18:19:09

我正在开发我在Windows 8中的第一个应用程序。我从Windows 8的第九频道看了教程http://channel9.msdn.com/Series/Windows-Store-apps-for-Absolute-Beginners-with-C-#我想做一个演示应用程序,其中的数据是通过web服务,但我不能使DataSource.cs文件的JSON数据,我通过web服务接收。

这是我的JSON数据通过Web Services..

[
    {
        "id": "1",
        "title": "Test_rom",
        "subtitle": "",
        "icon": "http://lpl.info/Admin/upload/cat.1.png"
    },
    {
        "id": "2",
        "title": "Jewelry",
        "subtitle": "",
        "icon": "http://lpl.info/Admin/upload/cat.2.png"
    },
    {
        "id": "3",
        "title": "Jackets",
        "subtitle": "All sizes available",
        "icon": "http://lpl.info/Admin/upload/cat.3.png"
    },
    {
        "id": "4",
        "title": "Dresses",
        "subtitle": "",
        "icon": "http://lpl.info/Admin/upload/cat.4.png"
    },
    {
        "id": "5",
        "title": "Bags",
        "subtitle": "",
        "icon": "http://lpl.info/Admin/upload/cat.5.png"
    },
    {
        "id": "6",
        "title": "Watches",
        "subtitle": "",
        "icon": "http://lpl.info/Admin/upload/cat.6.png"
    },
    {
        "id": "8",
        "title": "Glasses",
        "subtitle": "",
        "icon": "http://lpl.info/Admin/upload/cat.8.png"
    }
]

现在我想为这些组类别JSON数据做一个网格模板。但我是这个领域的新手,我试图将其与XAML UI绑定,但我失败了。

我试了很多找到一些相关的代码,但所有的代码都是为windows phone 8和我想为windows 8 Metro应用程序。

这些是在XAML的代码,我搞混了。

  <Page.Resources>
        <CollectionViewSource
            x:Name="groupedItemsViewSource"
            Source="{Binding Groups}"
            IsSourceGrouped="true"
            ItemsPath="Items"
            d:Source="{Binding Groups, Source={d:DesignInstance Type=local:RootObject, CreateList=True}}"/>
    </Page.Resources>

protected override void LoadState(Object navigationParameter, Dictionary<string,> pageState)
{
OsCatalogData os = new OsCatalogData();
os.GetJson(Constants.OscatalogMenulist, "HomePagemenu");
// TODO: Create an appropriate data model for your problem domain to replace the sample data
this.DefaultViewModel["Group"] = os.?????; 
}

我试着从这么多天发展它。有人能帮忙吗?我不想要完整的代码,但是一些材料,我可以在哪里满足我的要求....

Thanks in advance....

如何通过Web API(在线服务)绑定JSON数据与xaml UI在Windows应用商店

我从你的问题中理解-你在解析json数据时遇到了问题,你想使用。希望我是对的-

1 =>安装Newtonsoft。Json内核包。它将用于解析json有效和非常容易使用。

2 =>现在创建一个RootObject类(相应地命名-它是一个简单的数据解析器类)

public class RootObject
{
    public string id { get; set; }
    public string title { get; set; }
    public string subtitle { get; set; }
    public string icon { get; set; }
}

3 =>现在你的Json代表了这些类对象的列表/数组,你的Json类型实际上是列表,所以解析它像这样。

var groups= JsonConvert.DeserializeObject<List<RootObject>>(yourJson);

现在你的组以c#类的形式包含你的数据,现在你可以根据你的需要使用它们

阅读此http://prathapk.net/consuming-web-api-in-windows-phone-8/这个http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

PS:绑定时使用"Group"而不是"Groups"。我希望这对你有帮助。我来这里寻找类似的问题