2014-04-15 48 views
0

如何反序列化這個jsonx變量並將它放在windows phone上的longlistselector上?將一個JSON數組反序列化爲LongListSelector Windows Phone 8

目前我只能使用messagebox回顯一個字符串。我需要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': '[email protected]', 
    '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); 
      // [email protected] 
      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> 

回答

0

你需要一個類來表示你的JSON:

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

設置你的窗體中的ObservableCollection屬性:

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

然後MyClassCollection屬性設置爲反序列化JSON

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

這樣,你在你的類列表綁定到LongListSelector控制在你的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> 

那肩工作。

+0

它doesnt ...我不明白,我們應該使用list.ItemsSource?什麼是ObservableCollection – user3529738

+0

http://james.newtonking.com/json/help/index.html?topic=html/SerializingCollections.htm將有助於 – user3529738

+0

這是我的回答http://pastebin.com/CcADHaab – user3529738