简体   繁体   English

如何在Windows Store App中通过Web API(在线服务)和xaml UI绑定JSON数据?

[英]How to Bind JSON data through Web API(Online Services) with xaml UI In Windows Store App?

I am developing my first application in Windows 8. I saw tutorial from Channel 9 of Windows 8 http://channel9.msdn.com/Series/Windows-Store-apps-for-Absolute-Beginners-with-C-# I want to make a demo app where data is come through web services But I can't able to make DataSource.cs file for JSON Data Which I recieve through Web Service. 我正在Windows 8中开发我的第一个应用程序。我从Windows 8的第9频道看到了教程http://channel9.msdn.com/Series/Windows-Store-apps-for-Absolute-Beginners-with-C-#制作一个演示应用程序,其中数据通过Web服务传递,但是我无法为通过Web Service接收的JSON数据创建DataSource.cs文件。

This is my JSON Data come throgh Web Services.. 这是我的JSON数据来自Web服务。

[
    {
        "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"
    }
]

Now I want to make a Grid Template For these Group Category JSON DATA. 现在,我想为这些组类别JSON数据创建一个网格模板。 But I am new in this field and I tried to bind this with XAML UI But I failed. 但是,我是该领域的新手,因此尝试将其与XAML UI绑定,但是失败了。

I try a lot to find some related code But All code are for windows phone 8 and i want for windows 8 Metro Application. 我尝试了很多相关代码,但所有代码都适用于Windows Phone 8,而我想要适用于Windows 8 Metro Application。

These are the code in XAML Where i got confuse. 这些是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>

AND

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.?????; 

}

And I am trying to develop it from so many days. 我试图从很多天开始进行开发。 Can anyone Help?? 谁能帮忙? I don't want fully code But some material where can i fulfill my requirement.... 我不想要完整的代码,但是我可以在哪里满足我的要求...。

Thanks in advance.... 提前致谢....

What I undersatand from your question - you are having a problem in parsing the json data, that you want to use .Hope I will be right - 我对您的问题的理解是-您在解析要使用的json数据时遇到问题。希望我是对的-

1 => Install Newtonsoft.json nuget package. 1 =>安装Newtonsoft.json nuget软件包。 it will be used for parsing json efficiently and very easy to use. 它将用于高效且易于使用的json解析。

2 => Now create a RootObject Class ( Name it accordingly - it is a simple Data Parser Class ). 2 =>现在创建一个RootObject类(相应地命名-这是一个简单的Data Parser类)。

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

3 => Now your Json represent a List/Array of these Class Objects of What actually your json Type is List so parse it like this. 3 =>现在,您的Json表示这些类对象的列表/数组,实际上是您的json类型是List,所以可以这样解析。

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

Now your groups contains your data in the form C# Class now you can use them according to your need. 现在,您的组以C#类的形式包含数据,现在您可以根据需要使用它们。

Read this http://prathapk.net/consuming-web-api-in-windows-phone-8/ and this http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client 阅读此http://prathapk.net/using-web-api-in-windows-phone-8/和此http://www.asp.net/web-api/overview/advanced/calling-a-web-来自网络客户端的api

PS: Use "Group" and not "Groups" when you bind them. PS:绑定它们时,请使用“组”而不是“组”。 I hope this will help you. 我希望这能帮到您。 Got here searching for a similar problem. 在这里寻找类似的问题。

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

相关问题 如何绑定多行在C#/ XAML中的Windows应用商店应用中导致单个数据绑定 - How to bind muliple row result in single data binding in Windows store app in C#/XAML 如何在C#中为Windows Store应用程序编写嵌套的Json Web服务 - How to write nested Json web services in c# for windows store app Windows应用商店XAML Gridview无法呈现数据 - Windows Store App XAML Gridview not rendering data Windows应用商店-XAML-为Height属性绑定两个值 - Windows Store App - XAML - Bind two values for Height Property 为Windows应用商店打开Web服务? - open web services for Windows store app? Windows Store Universal 8.1 App如何绑定到共享区域中的数据 - Windows Store Universal 8.1 App How to Bind to data in Shared area Windows应用商店-XAML UI元素在C#代码中不可见 - Windows Store App - XAML UI element not visible in C# code 单元测试Windows 8 Store App UI(Xaml控件) - Unit Testing Windows 8 Store App UI (Xaml Controls) 如何在C#/ XAML Windows应用商店(Metro UI)应用程序中混合使用浅色和深色主题? - How do I mix Light and Dark themes in a C#/XAML Windows Store (Metro UI) App? 如何在Win Store App(8.1)中扩展Windows.UI.Xaml.Shapes.Shape? - How to extend Windows.UI.Xaml.Shapes.Shape in Win Store App (8.1)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM