2012-11-25 3 views
1

JSON 파일을 ListBox로 구문 분석하려고하지만이 작업을 수행하는 방법을 모르고 여기 및 다른 예제를 이해하는 데 어려움을 겪고 있습니다.JSON 파일을 ListBox로 구문 분석

현재, 나는 다음과 같습니다

XAML

<controls:Pivot Title="Episode Guide"> 
     <!--Pivot item one--> 
     <controls:PivotItem Header="season 1"> 
      <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
       <ListBox x:Name="Season1Box" Margin="0,0,0,0" VerticalAlignment="Top" Height="607" SelectionChanged="Season1Box_SelectionChanged"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
           <Image Source="{Binding Path=image1}"/> 
           <TextBlock Text="{Binding Path=title1}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" /> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </Grid> 
     </controls:PivotItem> 

     <!--Pivot item two--> 
     <controls:PivotItem Header="season 2"> 
      <Grid x:Name="ContentPanel2" Grid.Row="1" Margin="12,0,12,0"> 
       <ListBox x:Name="Season2Box" Margin="0,0,0,0" VerticalAlignment="Top" Height="607" SelectionChanged="Season2Box_SelectionChanged"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
           <Image Source="{Binding Path=image2}"/> 
           <TextBlock Text="{Binding Path=title2}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" /> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </Grid> 
     </controls:PivotItem> 

     <controls:PivotItem Header="season 3"> 
      <Grid x:Name="ContentPanel3" Grid.Row="1" Margin="12,0,12,0"> 
       <ListBox x:Name="Season3Box" Margin="0,0,0,0" VerticalAlignment="Top" Height="607" SelectionChanged="Season3Box_SelectionChanged"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
           <Image Source="{Binding Path=image3}"/> 
           <TextBlock Text="{Binding Path=title3}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" /> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </Grid> 
     </controls:PivotItem> 
    </controls:Pivot> 

C#

private void EpisodeGuideStream() 
    { 
     var client = new WebClient(); 
     client.OpenReadCompleted += 
      (s, eargs) => 
      { 
       var serializer = new DataContractJsonSerializer(typeof(RootObject)); 
       if (eargs.Error != null) 
       { 
        if (eargs.Error.Message.Contains("NotFound")) 
        { 
         MessageBox.Show("Could not retrieve episode guide", "Error", MessageBoxButton.OK); 
        } 
        else 
        { 
         MessageBox.Show("Could not retrieve episode guide", "Error", MessageBoxButton.OK); 
        } 
       } 
       else 
       { 
        var episodeGuide = (RootObject)serializer.ReadObject(eargs.Result); 

        //I have no idea what I'm doing (Part 1) 
        List<string> episode1 = new List<string>(); 
        List<string> title1 = new List<string>(); 
        List<string> director1 = new List<string>(); 
        List<string> writer1 = new List<string>(); 
        List<string> airdate1 = new List<string>(); 
        List<string> sypnosis1 = new List<string>(); 
        List<string> image1 = new List<string>(); 

        foreach (var obj in episodeGuide.SEASON1) 
        { 
         //I have no idea what I'm doing (Part 2) 
         episode1.Add(obj.EPISODE); 
         title1.Add(obj.TITLE); 
         director1.Add(obj.DIRECTOR); 
         writer1.Add(obj.WRITER); 
         airdate1.Add(obj.AIRDATE); 
         sypnosis1.Add(obj.SYPNOSIS); 
         image1.Add(obj.IMAGE); 

        //  Season1Box.ItemsSource = figure out what goes here; 
        } 
        foreach (var obj2 in episodeGuide.SEASON2) 
        { 
         //   populate Season2Box 
        } 
        foreach (var obj3 in episodeGuide.SEASON3) 
        { 
         //   populate Season3Box 
        } 



       } 
      }; 
     var uri = new Uri(jsonfile); 
     client.OpenReadAsync(uri); 
    } 

    public class SEASON1 
    { 
     public string EPISODE { get; set; } 
     public string TITLE { get; set; } 
     public string DIRECTOR { get; set; } 
     public string WRITER { get; set; } 
     public string AIRDATE { get; set; } 
     public string SYPNOSIS { get; set; } 
     public string IMAGE { get; set; } 
    } 

    public class SEASON2 
    { 
     public string EPISODE { get; set; } 
     public string TITLE { get; set; } 
     public string DIRECTOR { get; set; } 
     public string WRITER { get; set; } 
     public string AIRDATE { get; set; } 
     public string SYPNOSIS { get; set; } 
     public string IMAGE { get; set; } 
    } 

    public class SEASON3 
    { 
     public string EPISODE { get; set; } 
     public string TITLE { get; set; } 
     public string DIRECTOR { get; set; } 
     public string WRITER { get; set; } 
     public string AIRDATE { get; set; } 
     public string SYPNOSIS { get; set; } 
     public string IMAGE { get; set; } 
    } 

    public class RootObject 
    { 
     public List<SEASON1> SEASON1 { get; set; } 
     public List<SEASON2> SEASON2 { get; set; } 
     public List<SEASON3> SEASON3 { get; set; } 
    } 

    private void Season1Box_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     //To eventually navigate to another page 
    } 

    private void Season2Box_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     //To eventually navigate to another page 
    } 

    private void Season3Box_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     //To eventually navigate to another page 
    } 

JSON 파일이 올바르게 구문 분석하고 여기 비트의 데이터를 검색 할 수 있으며, 거기에,하지만 나는 하나의 ListBox에 그들을 모두 모으는 방법을 전혀 모른다.

내가하려고하는 것은 ListBox가 JSON의 "TITLE"옆에있는 JSON의 "IMAGE"를 표시하는 것입니다. 그들을 클릭하면 궁극적으로 다른 모든 정보가있는 새 페이지로 이동해야합니다. 잠재적으로 QueryStrings를 통해 모든 비트와 조각을 전달할 수 있습니다.

누군가 내 마지막 비트가 의미가 있고, ListBox의 올바른 방향으로 나를 가리키고 있는지 확인할 수 있다면 감사 할 것입니다.

답변

1
  1. 당신은 SEASON2SEASON3 클래스를 제거해야하고, EpisodeSEASON1 이름을 바꿉니다. 클래스 이름은 직렬화에 사용되지 않습니다. IMAGE- 속성을 Uri으로 변경하여 이미지에 바인딩 할 수도 있습니다.

    public class Episode 
    { 
        public string EPISODE { get; set; } 
        public string TITLE { get; set; } 
        public string DIRECTOR { get; set; } 
        public string WRITER { get; set; } 
        public string AIRDATE { get; set; } 
        public string SYPNOSIS { get; set; } 
        public Uri IMAGE { get; set; } 
    } 
    
    public class RootObject 
    { 
        public List<Episode> SEASON1 { get; set; } 
        public List<Episode> SEASON2 { get; set; } 
        public List<Episode> SEASON3 { get; set; } 
    } 
    
  2. 당신은 바인딩 (또는 지정) 목록 상자의 ItemsSource 재산 목록을, 그리고 그것은 목록에있는 개체를 기반으로 행이 채워 것입니다 수 있습니다. 각 목록 항목은 해당 객체를 DataContext로 사용하며 해당 속성에 바인딩 할 수 있습니다.

    Season1Box.ItemsSource = root.SEASON1; 
    Season2Box.ItemsSource = root.SEASON2; 
    Season3Box.ItemsSource = root.SEASON3; 
    
  3. 는 객체 속성을 컨트롤의 속성을 바인딩하려면 구문 Text="{Binding Path=MyProperty}"를 사용할 수 있습니다. MyProperty.OtherProperty과 같이 중첩 된 속성을 처리 할 수도 있습니다.

    <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
          <Image Source="{Binding Path=IMAGE}"/> 
          <TextBlock Text="{Binding Path=TITLE}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" /> 
         </StackPanel> 
        </DataTemplate> 
    </ListBox.ItemTemplate> 
    
+0

우수, 그했다. { 경우 (Season1Box.SelectedIndex == -1 는' 개인 무효 Season1Box_SelectionChanged code' (개체를 보낸 사람을, SelectionChangedEventArgs 전자) : 지금 새 페이지로 정보를 가져 오도록 노력하고있어, 나는 현재 다음과 같은 일을 해요) 반환; var myObj = 새 에피소드(); // myObj = Season1Box.SelectedIndex; // 위의 행을 작동시켜야합니다. – ReignOfComputer

+0

'code' // 이전 게시물에서 계속 NavigationService.Navigate (새 Uri ("/ EpisodeDetails.xaml? season = 1 & episode ="+ myObj.EPISODE + "& title ="+ & writer = "+ myObj.WRITER +"& airdate = "+ myObj.AIRDATE +"& director = "+ myObj.DIRECTOR +"& sypnosis = "+ myObj.SYPNOSIS, UriKind.Relative)); Season1Box.SelectedIndex = -1; } – ReignOfComputer

+0

여기에 붙여 넣기가 있습니다. http : // pastebin입니다.지금 어려움을 겪고있는 것은 다음 페이지로 전달할 값을 설정하는 방법을 모르겠다는 것입니다. 나는 그 잘못이 내 주석 처리 된 어딘가에 있다고 믿는다. 감사합니다. – ReignOfComputer