1

我正在尝试将 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 的“IMAGE”,旁边是来自 JSON 的“TITLE”。单击它们最终应该导航到包含所有其他信息的新页面,一旦我弄清楚这一点,我可能会通过 QueryStrings 或其他东西传递所有的点点滴滴。

如果有人可以检查我的最后一点是否有意义,并为我指出 ListBox 的正确方向,我将不胜感激。

4

1 回答 1

1
  1. 您应该摆脱SEASON2SEASON3类,并重命名SEASON1Episode. 序列化中不使用类名。您还应该将IMAGE-property 更改为 type 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. 您可以将列表绑定(或分配)到 ListBox 的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>
    
于 2012-11-25T23:04:21.793 回答