我有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;
}
}
Rating.cs:
class Rating
{
public string ID { get; set; }
public string Name { get; set; }
public override string ToString()
{
return this.Name;
}
}
Serial.cs与Rating.cs相同
我有一个问题,那就是如果我在组合框类别中选择了“ Usia”,那么该类别的组合框子类别可以在服务器上平滑地显示数据,然后我在组合框类别中选择了“串行”,就可以使用组合框该类别的子类别可以在服务器上顺利显示数据。然后,我再次在组合框类别中选择了“ Usia”,因此,当我单击子类别时,将显示一条错误消息,如下所示: 这是我遇到的问题的PC屏幕视频: https://1drv.ms/v/s!Auqiv8Ukng7UgP8Fj-LMDa9skV3yfA
在视频中,当在组合框类别中重新选择“ Usia”时(在视频末尾),它将显示一条错误消息,如上所示 如何处理?