You could refer to the following code to add data to ISO2. Since the data is too large, I tested 10 data.
Xaml:
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DataGrid x:Name="dg" ItemsSource="{Binding AirportsList}" Height="300" Grid.Column="1" Grid.Row="3"/>
AirportServices:
public async Task<List<Airport>?> GetAirportsAsync(string url)
{
if (airports?.Count > 0)
{
return airports;
}
using (var client = new HttpClient())
{
var content = await client.GetStringAsync(url);
airports = JsonConvert.DeserializeObject<List<Airport>?>(content);
MessageBox.Show(airports.Count.ToString());
//for (int i = 0; i < airports.Count; i++)
for (int i =0;i < 10;i++)
{
//var t = Task.Run(() => GetCountryCodeAsync($"https://countrycode.dev/api/countries"));
//if(t!=null)
//{
// var list = JsonConvert.DeserializeObject<List<string>?>(t.Result);
// airports[i].ISO2 = list[i].ToString();
//}
var response = string.Empty;
using (var client1 = new HttpClient())
{
HttpResponseMessage result = await client1.GetAsync($"https://countrycode.dev/api/countries");
if (result.IsSuccessStatusCode)
{
response = await result.Content.ReadAsStringAsync();
var list = JsonConvert.DeserializeObject<List<string>>(response);
airports[i].ISO2 = list[i].ToString();
}
}
}
return airports;
}
return null;
}
public async Task<string?> GetCountryCodeAsync(string url)
{
var response = string.Empty;
using (var client = new HttpClient())
{
HttpResponseMessage result = await client.GetAsync(url);
if (result.IsSuccessStatusCode)
{
response = await result.Content.ReadAsStringAsync();
return response;
}
return null;
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.