public async Task <PokemonList> GetPokemonList(string Url) { using (HttpClient client = new HttpClient()) { using (HttpResponseMessage response = await client.GetAsync(Url)) { using (HttpContent content = response.Content) { var data = await content.ReadAsStringAsync(); PokemonList pokemonList = JsonConvert.DeserializeObject <PokemonList>(data); return(pokemonList); } } } }
public MainWindow() { InitializeComponent(); string baseUrl = "https://pokeapi.co/api/v2/pokemon?limit=100"; try { pokemonList = Task.Run(() => GetPokemonList(baseUrl)).Result; buttons = new ObservableCollection <PokemonButton>(); foreach (NameAndUrl pokemon in pokemonList.results) { buttons.Add(new PokemonButton { ButtonContent = char.ToUpper(pokemon.name[0]) + pokemon.name.Substring(1), Url = pokemon.url }); } ic.ItemsSource = buttons; } catch (Exception ex) { Console.WriteLine(ex); } }