将 JSON 数组反序列化为 LongListSelector Windows Phone 8 [英] Deserializing a JSON array to LongListSelector Windows Phone 8

查看:22
本文介绍了将 JSON 数组反序列化为 LongListSelector Windows Phone 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何反序列化这个 jsonx 变量并将其放在 windows 手机上的 longlistselector 上?

How can i deserialize this jsonx variable and put it on a longlistselector on windows phone?

目前我只能用消息框回显一个字符串.我需要在 longlistselector 上设置键值.

Currently i can only echo one string with messagebox. i need key-value sets on a longlistselector.

这是我的答案http://pastebin.com/CcADHaab

这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp5.Resources;
using Newtonsoft.Json;

namespace PhoneApp5
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();



            string json = @"{
  'Email': 'james@example.com',
  'Active': true,
  'CreatedDate': '2013-01-20T00:00:00Z'
}";

            string jsonx = @"{
  'Table1': [
    {
      'id': 0,
      'item': 'item 0'
    },
    {
      'id': 1,
      'item': 'item 1'
    }
  ]
}";



            Account account = JsonConvert.DeserializeObject<Account>(json);

           // Console.WriteLine(account.Email);
            // james@example.com
            MessageBox.Show(account.Email);

            list.ItemsSource = new BookList();

        }



        }
    public class BookList : List<Account>
    {
        public BookList()
        {


        }


    }
        public class Account
        {
            public string Email { get; set; }
            public bool Active { get; set; }
            public DateTime CreatedDate { get; set; }
           // public IList<string> Roles { get; set; }

        }

    ///////////////////////////







}

和我的 XAML

<phone:PhoneApplicationPage
    x:Class="PhoneApp5.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>



        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
            <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <phone:LongListSelector Name="list">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding id}" />
                            <TextBlock Text=" ( " />
                            <TextBlock Text="{Binding item}" FontStyle="Italic" />
                            <TextBlock Text=" )" />
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </Grid>


    </Grid>

</phone:PhoneApplicationPage>

推荐答案

您需要一个类来表示您的 json:

You need a class to represent your json:

public class MyClass
{
   public string Id { get; set; }
   public string Item { get; set; }
}

在表单中设置 ObservableCollection 属性:

Set a ObservableCollection property in your Form:

public ObservableCollection<MyClass> MyClassCollection { get; set; }

然后将 MyClassCollection 属性设置为您的反序列化 json

and then set the MyClassCollection property to your Deserialized json

MyClassCollection = JsonConvert.DeserializeObject<ObservableCollection<MyClass>>(e.Result);

这样您就可以将类列表绑定到 XAML 中的 LongListSelector 控件:

That way you're binding your class list to the LongListSelector Control in your XAML:

        <phone:LongListSelector Name="list" ItemsSource="{Binding MyClassCollection}">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding id}" />
                        <TextBlock Text=" ( " />
                        <TextBlock Text="{Binding item}" FontStyle="Italic" />
                        <TextBlock Text=" )" />
                    </StackPanel>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>

应该可以.

这篇关于将 JSON 数组反序列化为 LongListSelector Windows Phone 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆