async void searchButton_Clicked(object sender, EventArgs e)
        {
            if (nameEntry.Text != null)
            {
                //getting input and constructing request url
                string userInput;
                userInput = nameEntry.Text.ToLower();

                //seting up URI and creatting HTTP client and response holder
                string              nameApiEndpoint = globals.GlobalVariables.searchByNameString + userInput;
                Uri                 nameApiUri      = new Uri(nameApiEndpoint);
                HttpClient          client          = new HttpClient();
                HttpResponseMessage response        = await client.GetAsync(nameApiUri);

                //response stream into string
                string jsonContent = await response.Content.ReadAsStringAsync();

                if (jsonContent != globals.GlobalVariables.notFoundString)
                {
                    //parse into an object of type PokemonJson
                    var pokemon = PokemonJson.FromJson(jsonContent);

                    //sending the object to the next page
                    await Navigation.PushAsync(new PokemonPage(pokemon));
                }
                else //pop up message if the pokemon doesn't exist
                {
                    await DisplayAlert("NOT FOUND", "Please try again!", "OK");
                }
            }
            else
            {
                await DisplayAlert("EMPTY FIELD", "Please enter a pokemon!", "OK");
            }
        }
Exemplo n.º 2
0
        async void openPokemonPage(string nameOrNum)
        {
            // sets up URI and creates HTTP client and response holder.
            string              nameApiEndpoint = globals.GlobalVariables.searchByNameString + nameOrNum;
            Uri                 nameApiUri      = new Uri(nameApiEndpoint);
            HttpClient          client          = new HttpClient();
            HttpResponseMessage response        = await client.GetAsync(nameApiUri);

            //response stream into string
            string jsonContent = await response.Content.ReadAsStringAsync();

            if (jsonContent != globals.GlobalVariables.notFoundString)
            {
                //parses into an object of type PokemonJson
                var pokemon = PokemonJson.FromJson(jsonContent);

                globals.GlobalVariables.searchHistory.Add(pokemon);

                //sends the object to the next page.
                await Navigation.PushAsync(new PokemonPage(pokemon, globals.GlobalVariables.endpoint));
            }
        }