c# - 具有多个组合框子类别的多个组合框类别
问题描述
我有 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# - ASP.NET CORE 2 System.Net.Sockets.SocketException:每当上传文件时
- ruby - 当我尝试在 ruby 中复制工作表 google-api-client-0.30.2 时出现“服务器错误(Google::Apis::ServerError)”
- javascript - 渲染从 JSX 后端获取的 HTML 标记
- opengl - Vulkan 中的 OpenGL GLSL 原子计数器
- python-3.x - 通过 SQLAlchemy 方言查询 Crate 数据库:没有更多可用的服务器
- android - 仅当 ROOM 数据库中的行被删除时,如何显示 Toast?
- ios - 获取自己的蓝牙唯一 ID
- c# - 用于查找我的设备是否支持蜂窝连接的 API
- node.js - '@sailshq/socket.io-redis@latest' 不在 npm 注册表中
- javascript - Laravel 5.8 Pure Javascript Fetch AJAX PromiseStatus 返回被拒绝