我正在尝试将 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 的正确方向,我将不胜感激。