// the api calls have to be asynchronous methods, so they can't be used in a constructor. Instead, I created a public static async // method that in turn creates and returns an instance of Page2, so it functions as kind of an async constructor public static async Task <Page2> Create(string summonerName, Region region, string apiKey, string txtFileLoc) { // create an instance of this class (Page2) to be returned var page = new Page2(); // set the api from the input in page 1 if it isn't the default or empty if (!apiKey.Equals("api key") && !apiKey.Equals("")) { page.api = RiotApi.GetDevelopmentInstance(apiKey); page.returnApiKey = apiKey; } // set the file location from page 1 input if it isn't the default or empty if (!txtFileLoc.Equals("txt file") && !txtFileLoc.Equals("")) { page.noteFileLocation = txtFileLoc; } // get the region for returning to page 1 // Na = 3, Euw = 2, Kr = 4 page.returnRegion = (int)region; // get summoner using input and riot API var summoner = page.getSummoner(summonerName, region); // display name of summoner page.summonerNameDisplay.Text = summoner.Name; //get the info for the current game of the summoner await page.getCurrentGame(summoner, region); // set the pictures for the summonerGroups page.setImageGroups(); // watch the file for updates, and if detected, update the app accordingly page.runWatch(); // return the instance of Page2 return(page); }
//execute the search based on inputted summoner name and selected region private async void Button_Click(object sender, RoutedEventArgs e) { messageBox.Text = "Loading..."; //make the message box convey that the program is executing the search string summonerName = summonerNameInput.Text; //set the summoner name to be searched from what the user inputted if (summonerName != "") // if the input box isn't empty { Region region = Region.Na; //default region is Na (North America) //set the server region based on what radio button is selected (Na = North America, Euw = Western Europe, Kr = South Korea) if (NAButton.IsChecked == true) { region = Region.Na; } else if (EUWButton.IsChecked == true) { region = Region.Euw; } else if (KRButton.IsChecked == true) { region = Region.Kr; } //try to execute the search using the summoner name and region try { this.NavigationService.Navigate(await Page2.Create(summonerName, region, apiKeyBox.Text, textFileLocationBox.Text)); //if successful, page 2 will be navigated to }//if search fails, update message box to say why based on exception received catch (Exception ex) { messageBox.Text = ex.Message; Console.WriteLine(ex.StackTrace); } } else { messageBox.Text = ""; //clear message box if box was empty } }