public GamePage(QuizGame GameArg) { InitializeComponent(); SelectedIndex = -1; Game = GameArg; CategoryLabel.Text = HttpUtility.HtmlDecode(Game.Category); QuestionLabel.Text = HttpUtility.HtmlDecode(Game.Question); foreach (string answer in Game.Answers) { Button AnswerBtn = new Button(); AnswerBtn.Text = HttpUtility.HtmlDecode(answer); AnswerBtn.TextColor = Color.GhostWhite; AnswerBtn.CornerRadius = 50; AnswerBtn.BackgroundColor = Color.Transparent; AnswerBtn.BorderWidth = 3; AnswerBtn.BorderColor = Color.GhostWhite; AnswerBtn.Margin = new Thickness(30, 0, 30, 5); AnswerBtn.Clicked += Answer_Clicked; AnswersLayout.Children.Add(AnswerBtn); } }
private async void Start_Game(object sender, EventArgs e) { LoadingIndicator.IsRunning = true; HttpResponseMessage Response; string Difficulty = DifficultyIndex == 3 ? "" : "&difficulty=" + Difficulties[DifficultyIndex].ToLower(); string Category = CategoryIndex == -1 ? "" : "&category=" + CategoryIndex; try { Response = await HTTPClient.GetAsync(string.Format("https://opentdb.com/api.php?type=multiple&amount=1&{0}{1}", Difficulty, Category)); } catch (Exception E) { await DisplayAlert("Alert", "Couldn't get the game details!\nReason: " + E.Message, "Okay"); LoadingIndicator.IsRunning = false; return; } String RawContent = await Response.Content.ReadAsStringAsync(); JObject JSON = JObject.Parse(RawContent); JObject Quiz = (JObject)JSON.GetValue("results").First; List <string> Questions = Quiz.GetValue("incorrect_answers").ToObject <List <string> >(); String CorrectQuestion = Quiz.Value <string>("correct_answer"); Questions.Add(CorrectQuestion); Questions = Questions.OrderBy(element => Guid.NewGuid()).ToList <string>(); QuizGame Game = new QuizGame(Questions, CorrectQuestion); Game.Category = (string)Quiz.GetValue("category"); Game.Question = (string)Quiz.GetValue("question"); await Navigation.PushModalAsync(new GamePage( Game )); LoadingIndicator.IsRunning = false; }