在 Gridview 内的 Gridview 上显示 Json

Display Json on Gridview inside A Gridview

我有一个Json,如下图:

我想在 gridview 内的 gridview 中显示“专业”

XAML:

<GridView x:Name="asesmenGridView">
    <GridView.ItemTemplate>
        <DataTemplate>
            <Grid x:Name="asesmenGrid"> 
                <TextBlock  
                    x:Name="judulT"
                    Text="{Binding Title}"/>
                <GridView
                    x:Name="majorGridView"
                                        ItemsSource="{Binding DataContext.majordatasource, ElementName=asesmenGridView}">
                        <GridView.ItemTemplate>
                                                    <DataTemplate x:DataType="local:MajorClass">
                                <Grid>
                                                                        <TextBlock
                                                                            x:Name="kelasT"
                                                                            Text="{x:Bind Major}"  />
                                                                </Grid>
                            </DataTemplate>
                                                    </GridView.ItemTemplate>
                                                </GridView>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

代码:

    public MainPage()
                {
                    this.InitializeComponent();
                this.DataContext = this;
                }
        public List<MajorClass> majordatasource { get; set; } = new List<MajorClass>();    
        string urlPath = "https://.../choose-exam/2";
                                    var httpClient = new HttpClient(new HttpClientHandler());
                                    httpClient.DefaultRequestHeaders.Add("Authorization",
                                        string.Format("Bearer {0}", tkn));
                                    var response = await httpClient.GetAsync(urlPath);
             string jsonText = await response.Content.ReadAsStringAsync();
                                    try
                                    {
                                        JsonArray jsonArray = JsonArray.Parse(jsonText);
                                        foreach (JsonValue groupValue in jsonArray)
                                        {
                                            string title = groupObject["title"].GetString();
             JsonArray majorArray = groupObject["majors"].GetArray();
                                            MajorClass majorC = new MajorClass();
                                            foreach (JsonValue majorValue in majorArray)
                                            {
                                                JsonObject majorObject = majorValue.GetObject();
                                                double majorID = majorObject["major_id"].GetNumber();
                                                string major = majorObject["major"].GetString();
            
                                                majorC.MajorID = (int)majorID;
                                                majorC.Major = major + ",";
                                                majordatasource.Add(majorC);
                                            }
Ujian ujian = new Ujian();
                                    ujian.Title = title;
             ujiandatasource.Add(ujian);
            }
            asesmenGridView.ItemsSource = ujiandatasource;

乌建Class:

public class Ujian
    {
        public string Title { get; set; }
        public ObservableCollection<MajorClass> majordatasource { get; set; }

        public Ujian()
        {
            Title = string.Empty;
            majordatasource = new ObservableCollection<MajorClass>();
        }

        public Ujian(string title)
        {
            Title = title;
        }
    }

    public class MajorClass
    {
        public int MajorID { get; set; }
        public string Major { get; set; }
    }
}

从上面的代码来看,我在一个gridview中并没有成功显示到gridview上(取到的数据只是最后一条数据,重复显示,如下图)

如何处理?

根据你的设计,你需要nest list数据结构,majorGridView可以直接访问majordatasource 属性乌建。请将主要数据源传递给 ujian.majordatasource,但不要在页面 class.

中访问单独的主要数据源 属性

例如

Ujian ujian = new Ujian();
ujian.Title = title;
ujian.majordatasource = majordatasource;
ujiandatasource.Add(ujian)

Xaml

<GridView
 x:Name="majorGridView"
 ItemsSource="{Binding majordatasource}">

更新

try
{
    JsonArray jsonArray = JsonArray.Parse(jsonText);
    foreach (JsonValue groupValue in jsonArray)
    {
        var groupObject = groupValue.GetObject();
        string title = groupObject["title"].GetString();
        JsonArray majorArray = groupObject["majors"].GetArray();
        var majordatasource = new ObservableCollection<MajorClass>();
       
        foreach (JsonValue majorValue in majorArray)
        {
            MajorClass majorC = new MajorClass();
            JsonObject majorObject = majorValue.GetObject();
            double majorID = majorObject["major_id"].GetNumber();
            string major = majorObject["major"].GetString();

            majorC.MajorID = (int)majorID;
            majorC.Major = major + ",";
            majordatasource.Add(majorC);
        }


        Ujian ujian = new Ujian();
        ujian.majordatasource = majordatasource;
        ujian.Title = title;
        ujiandatasource.Add(ujian);
    }
}
catch
{

}