Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

True/False Quiz: How to show correct answers or "errors"

$
0
0

I am working on a True/False Quiz using Xamarin Forms. I want to display the "errors" either at the end of the quiz (ResultsPage) or in between questions. I was thinking on adding the incorrect questions into an array/list so that I can display later, but how do I do this? What is the best way to go about this?

Here's the template of the open-source code I found on Github:

public ResultsPage(int score, int total)
{
InitializeComponent();

        ScoreText.Text = $"{score} out of {total}";
    }

        private void Summary(object sender, EventArgs e)
        {
            Navigation.PopToRootAsync();


        }
}

  class QuizPageModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        public List<Question> questions;

        public List<Question> errors;

        private string _questiontext;

        public string QuestionText
        {
            get
            {
                return _questiontext;
            }

            set
            {
                _questiontext = value;
                OnPropertyChanged();
            }
        }

        private bool _currentAnswerValue;
        public bool CurrentAnswerValue
        {
            get
            {
                return _currentAnswerValue;
            }

            set
            {
                _currentAnswerValue = value;
                OnPropertyChanged();
            }
        }

        private int _totalQuestions;
        public int TotalQuestions
        {
            get
            {
                return _totalQuestions;
            }

            set
            {
                _totalQuestions = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(TitleText));
            }
        }

        private int _currentQuestionNumber;
        public int CurrentQuestionNumber
        {
            get
            {
                return _currentQuestionNumber;
            }

            set
            {
                _currentQuestionNumber = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(TitleText));
            }
        }

        public string TitleText
        {
            get { return $"Question {_currentQuestionNumber} of {_totalQuestions}"; }
        }

        private int score;

        private Random random;

        public Command AnsweredTrue { get; }
        public Command AnsweredFalse { get; }
        public string QuestionText { get; }

        public QuizPageModel()
        {
            // initialise RNG
            random = new Random();


            questions = new List<Question>()
            {
                new Question() { QuestionText=" 1+1=3", Answer=false } ,
                new Question() {  QuestionText="2+2=4", Answer=true },
                new Question() {  QuestionText="3*5=18", Answer=false },
                new Question() {  QuestionText="2-1=1", Answer=true },


            };


            // initialise quiz values
            TotalQuestions = questions.Count;
            CurrentQuestionNumber = 1;
            score = 0;

            // load first question
            LoadQuestion();



            //True/False

            AnsweredTrue = new Command(async () =>
            {
                Debug.WriteLine("True button pressed");

                // check if answer is correct
                if (_currentAnswerValue == true)

                    score++;



                // load next question or results page
                if (CurrentQuestionNumber < TotalQuestions)
                {
                    // increase question counter
                    CurrentQuestionNumber++;
                    LoadQuestion();
                }
                else
                {
                    Debug.WriteLine("End of Quiz");
                    await ShowResults().ConfigureAwait(false);
                }
            });



            AnsweredFalse = new Command(async () =>
            {
                Debug.WriteLine("False button pressed");



                // check if answer is correct
                if (_currentAnswerValue == false) score++;



                // load next question or results page
                if (CurrentQuestionNumber < TotalQuestions)
                {
                    // increase question counter
                    CurrentQuestionNumber++;
                    LoadQuestion();
                }
                else
                {
                    Debug.WriteLine("End of Quiz");
                    await ShowResults().ConfigureAwait(false);
                }
            });
        }


        private void LoadQuestion()
        {
            var index = random.Next(questions.Count);
            QuestionText = questions[index].QuestionText;
            CurrentAnswerValue = questions[index].Answer;
            questions.RemoveAt(index);
        }

        private async Task ShowResults() => await Application.Current.MainPage.Navigation.PushAsync(new ResultsPage(score, _totalQuestions)).ConfigureAwait(false);

    }
}

Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>