【问题标题】:How to Bind JSON data through Web API(Online Services) with xaml UI In Windows Store App?如何在 Windows Store App 中通过 Web API(在线服务)与 xaml UI 绑定 JSON 数据?
【发布时间】:2014-10-17 05:02:28
【问题描述】:

我正在 Windows 8 中开发我的第一个应用程序。我在 Windows 8 的第 9 频道看到了教程http://channel9.msdn.com/Series/Windows-Store-apps-for-Absolute-Beginners-with-C-# 我想制作一个演示应用程序,其中数据来自 Web 服务但我无法为我通过 Web 服务接收的 JSON 数据制作 DataSource.cs 文件。

这是我的 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"
    }
]

现在我想为这些组类别 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.?????; 

}

我正在尝试从这么多天开始开发它。谁能帮忙?? 我不想要完整的代码但是我可以在哪里满足我的要求的一些材料......

提前谢谢....

【问题讨论】:

    标签: c# xaml windows-8 json.net asp.net-web-api


    【解决方案1】:

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

    1 => 安装 Newtonsoft.json nuget 包。它将用于高效地解析 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 类型是 List,所以像这样解析它。

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

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

    【讨论】:

    • 我做了所有的事情......我的问题是 => 我在使用 xaml 跨度>
    【解决方案2】:

    阅读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”。 我希望这能帮到您。来到这里寻找类似的问题。

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 2011-03-18
      • 2013-01-10
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多