首页 > 解决方案 > 具有多个组合框子类别的多个组合框类别

问题描述

我有 2 个包含 2 个类别和 2 种子类别的组合框。组合框类别包含“Usia”和“Serial”。而子类别组合框从 json 服务器检索数据。如果在组合框类别中选择“Usia”,则会在组合框子类别中显示“ratingBox”,如果在组合框类别中选择“Serial”,则会在组合框子类别中显示“serialBox”。

XAML:

<StackPanel x:Name="comboBoxStack" Grid.Row="0" Margin="30,15,0,0" Orientation="Horizontal">
                <ComboBox x:Name="kategoriBox" PlaceholderText="Kategori" FontSize="14" SelectionChanged="KategoriBox_SelectionChanged">
                    <ComboBoxItem>Usia</ComboBoxItem>
                    <ComboBoxItem>Serial</ComboBoxItem>
                </ComboBox>
                <ComboBox x:Name="ratingBox" Margin="15,0,0,0" PlaceholderText="Pilih Usia" ItemsSource="{x:Bind RatingList}" FontSize="14" SelectionChanged="RatingBox_SelectionChanged"/>
                <ComboBox x:Name="serialBox" Margin="15,0,0,0" PlaceholderText="Pilih Serial" ItemsSource="{x:Bind SerialList}" FontSize="14" SelectionChanged="SerialBox_SelectionChanged"/>
            </StackPanel>

代码:

List<Rating> RatingList = new List<Rating>();
List<Serial> SerialList = new List<Serial>();
string umurID = "";
string serialID = "";

private void KategoriBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var comboBoxItem = kategoriBox.Items[kategoriBox.SelectedIndex] as ComboBoxItem;
            if (comboBoxItem != null)
            {
                string selectedcmb = comboBoxItem.Content.ToString();
                if(selectedcmb == "Usia")
                {
                    ratingBox.Visibility = Visibility.Visible;
                    serialBox.Visibility = Visibility.Collapsed;
                    RatingC();
                    ratingBox.SelectedIndex = -1;
                }
                else if(selectedcmb == "Serial")
                {
                    ratingBox.Visibility = Visibility.Collapsed;
                    serialBox.Visibility = Visibility.Visible;
                    SerialC();
                    serialBox.SelectedIndex = -1;
                }
            }
            else
            {
                ratingBox.Visibility = Visibility.Visible;
                serialBox.Visibility = Visibility.Collapsed;
                RatingC();
            }
        }

        private async void RatingC()
        {
            serialID = "";
            ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
            if (connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
            {
                try
                {
                    string urlPath = "https://..../Fetch/rating";
                    var httpClient = new HttpClient(new HttpClientHandler());

                    var values = new List<KeyValuePair<string, string>>
                    {

                    };

                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("SCH-API-KEY", "SCH_KEnaBiDeplebt");
                    var response = await httpClient.PostAsync(urlPath, new FormUrlEncodedContent(values));
                    response.EnsureSuccessStatusCode();

                    if (!response.IsSuccessStatusCode)
                    {
                        busyindicator.IsActive = false;
                    }

                    string jsonText = await response.Content.ReadAsStringAsync();
                    JsonObject jsonObject = JsonObject.Parse(jsonText);
                    JsonArray jsonData = jsonObject["data"].GetArray();

                    foreach (JsonValue groupValue in jsonData)
                    {
                        JsonObject groupObject1 = groupValue.GetObject();
                        string id = groupObject1["id"].GetString();
                        string name = groupObject1["rating"].GetString();

                        Rating rate = new Rating();
                        rate.ID = id;
                        rate.Name = name;

                        RatingList.Add(new Rating()
                        {
                            ID = rate.ID,
                            Name = rate.Name
                        });
                    }

                }
                catch
                {
                    busyindicator.IsActive = false;
                }
            }
        }

        private async void SerialC()
        {
            umurID = "";
            ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
            if (connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
            {
                try
                {
                    string urlPath = "https://.../Fetch/serial";
                    var httpClient = new HttpClient(new HttpClientHandler());

                    var values = new List<KeyValuePair<string, string>>
                    {

                    };

                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("SCH-API-KEY", "SCH_KEnaBiDeplebt");
                    var response = await httpClient.PostAsync(urlPath, new FormUrlEncodedContent(values));
                    response.EnsureSuccessStatusCode();

                    if (!response.IsSuccessStatusCode)
                    {
                        busyindicator.IsActive = false;
                    }

                    string jsonText = await response.Content.ReadAsStringAsync();
                    JsonObject jsonObject = JsonObject.Parse(jsonText);
                    JsonArray jsonData = jsonObject["data"].GetArray();

                    foreach (JsonValue groupValue in jsonData)
                    {
                        JsonObject groupObject1 = groupValue.GetObject();
                        string id = groupObject1["id"].GetString();
                        string name = groupObject1["nama"].GetString();
                        string slug = groupObject1["slug"].GetString();

                        Serial seri = new Serial();
                        seri.ID = id;
                        seri.Name = name;
                        seri.Slug = slug;

                        SerialList.Add(new Serial()
                        {
                            ID = seri.ID,
                            Name = seri.Name,
                            Slug = seri.Slug
                        });
                    }

                }
                catch
                {
                    busyindicator.IsActive = false;
                }
            }
        }

        private void RatingBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(ratingBox.SelectedIndex != -1)
            {
                var comboBox = sender as ComboBox;
                Rating value = comboBox.SelectedItem as Rating;
                umurID = value.ID;
            }
        }

        private void SerialBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(serialBox.SelectedIndex != -1)
            {
                var comboBox = sender as ComboBox;
                Serial value = comboBox.SelectedItem as Serial;
                serialID = value.ID;
            }
        }

评分.cs:

class Rating
    {
        public string ID { get; set; }

        public string Name { get; set; }

        public override string ToString()
        {
            return this.Name;
        }
    }

Serial.cs 与 Rating.cs 相同

我有一个问题,就是如果我在combobox类别中选择了“usia”,那么该类别的combobox子类别可以在服务器上顺利显示数据,然后我在combobox类别中选择“Serial”,为那个吃combobox子类别category 可以流畅地在服务器上显示数据。然后我再次在组合框类别中选择了“Usia”,所以当我点击子类别时,会出现如下所示的错误消息: 错误信息 这是我的问题的 PC 屏幕视频: https ://1drv.ms/v/s !Auqiv8Ukng7UgP8Fj-LMDa9skV3yfA

视频中在combobox类别中重新选择“Usia”时(视频末尾),会显示如上图的错误信息如何处理?

标签: c#jsonuwpcombobox

解决方案


推荐阅读