Hi. I'm trying to pass a List from one view to another, using the MVVM pattern. In my project, I want to store the questions from a List (questions) into another list (errores) and I want to display "errores" in another view. Basically, whenever the wrong True/False button is pressed, I want to store the question into "errores".
Here's what I've tried:
{ private List<string> errores; public RESULTSPAGE2(List<string> errores) { InitializeComponent(); MainListView.ItemsSource = errores; } } } //RESULTSPAGE2.xaml <ContentPage.Content> <StackLayout> <ListView x:Name="MainListView" /> </StackLayout> </ContentPage.Content> </ContentPage> class QuizPageModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged([CallerMemberName] string name = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } public List<Question> questions; List<string> errores = new List<string>(); private string _textopregunta; public string TextoPregunta { get { return _textopregunta; } set { _textopregunta = value; OnPropertyChanged(); } } //Add Color /* private Color _BackgroundFalseButton; public Color BackgroundFalseButton { get { return _BackgroundFalseButton; } set { _BackgroundFalseButton = value; //You must implement INotifyChanged OnPropertyChanged(nameof(BackgroundFalseButton)); } }*/ private bool _wrongAnswer; public bool WrongAnswer { get { return _wrongAnswer; } set { _wrongAnswer = 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(); // populate question list - replace with external data source in production questions = new List<Question>() { new Question() { QuestionText="1=2", Answer=false } , new Question() { QuestionText="2+2=3", Answer=false }, .......... }; // initialise quiz values TotalQuestions = questions.Count; CurrentQuestionNumber = 1; score = 0; // load first question LoadQuestion(); //True/False AnsweredTrue = new Command<Question>(async (question) => { Debug.WriteLine("True button pressed"); /* AnsweredTrue = new Command(async () => { Debug.WriteLine("True button pressed");*/ // check if answer is correct if (_currentAnswerValue == true) score++; else { errores.Add(QuestionText); } // 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++; else { errores.Add(QuestionText); //_wrongAnswer = true; } /* if (_wrongAnswer == true) { errores.Add(QuestionText); } */ // 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); TextoPregunta = questions[index].QuestionText; CurrentAnswerValue = questions[index].Answer; questions.RemoveAt(index); } // private async Task ShowResults() => await Application.Current.MainPage.Navigation.PushAsync(new LVTest(score, _totalQuestions, errores)).ConfigureAwait(false); private async Task ShowResults() => await Application.Current.MainPage.Navigation.PushAsync(new RESULTSPAGE2(errores)); }
}