0

Background:

In an attempt to learn WP 8.1 development (non SL) I am building a Quiz app aimed at children. It is a Fruit guessing game.

How it ought to work:

User is shown a picture of a fruit along with three words. One of those words is the name of the fruit. If the correct word for the fruit is chosen, then the user gets a point and is also shown the next fruit with word options.

What's the problem:

I am new to WP 8.1 development and I am not really sure how to swap the bound QuestionVM.cs on the XAML page when the User selects the correct answer. Here is a diagram I put together of what I currently have:

QuestionVM binding

That whole QuestionVM needs replacing when the user selects the correct answer.

How do I do that?

Is there even a way to do that? Or must I change the actual properties on the QuestionVM.cs rather than swapping the whole QuestionVM.cs instance?

halfer
  • 19,824
  • 17
  • 99
  • 186
J86
  • 14,345
  • 47
  • 130
  • 228
  • i wondering if you want to give the user the next task after the correct input? – POQDavid Jul 04 '15 at 13:16
  • I'm not sure what you mean? Eventually, there would be a bar at the bottom telling the user how many points they have. Does that make it clearer? – J86 Jul 04 '15 at 13:39
  • sorry but what is your error? – POQDavid Jul 04 '15 at 13:42
  • My question doesn't have an error. I just need to know how I can swap the ViewModel instance attached to the XAML. – J86 Jul 04 '15 at 13:45
  • Oh like dynamic XAML like each page have a new XAML? – POQDavid Jul 04 '15 at 13:46
  • Nope, One XAML, but multiple instances of the ViewModel `QuestionVM.cs`. – J86 Jul 04 '15 at 13:48
  • Well i found few things that i hope it help you http://stackoverflow.com/questions/5379998/multiple-instances-of-a-viewmodel http://stackoverflow.com/questions/13039126/how-to-have-multiple-unique-instances-of-viewmodel-using-mvvm-light http://stackoverflow.com/questions/10914860/how-to-have-multiple-instances-of-the-same-viewmodel-in-mvvm-light – POQDavid Jul 04 '15 at 13:58

1 Answers1

0
  1. Create a MainVM on top of your QuestionVM. This will be the back store of your main screen.
  2. Create a private List<QuestionVM> in MainVM. This will store all question objects in it.
  3. Create a readonly CurrentQuestion property of type QuestionVM. This will point to the current question object.
  4. Keep an private integer index for the current question.
  5. Create MoveNext, MovePrevious commands (RelayCommand type) in MainVM. Call these upon the selection of correct answer (or through explicit buttons whatever you need).
  6. These commands will simply increment/decrement the current index and then call RaisePropertyChanged on CurrentQuestion property. This will update your UI.

Note: I'm assuming you're using MVVM Light or some other MVVM framework and that your VM classes inherit from ViewModelBase or ObservableObject.

dotNET
  • 33,414
  • 24
  • 162
  • 251