-1

I have two View Models, MainMenuViewModel and ListViewModel. MainMenuViewModel has the property Page which is a string with the name of a xaml file to change the Source of a Frame. Now I want to change this property from the ListViewModel. I'm not using any framework. What I'm doing right now is to create a variable public MainMenuViewModel mmvm { get; set; } and calling a method from this variable to change the Page value, but is throwing System.NullReferenceException: 'Object reference not set to an instance of an object.'. How can I do this?

Paul Miranda
  • 578
  • 11
  • 27
  • Fire up your debugger and figure out why it is null. See https://stackoverflow.com/q/4660142/324260. – Ilian Dec 13 '19 at 00:03
  • How are you instantiating the `ListViewModel`? Please edit your question with the details. – mm8 Dec 13 '19 at 14:28

1 Answers1

0

A couple of options:

  1. If MainViewModel and ListViewModel have a direct relationship, you can just pass a reference to MainViewModel when constructing ListViewModel. Then just call MainViewModel's property directly from ListViewModel.

  2. If they don't have a direct relationship, you can use the Messenger pattern. Basically, MainViewModel will register to a type of message (e.g ChangePageMessage) and then ListViewModel will send an instance of that message to change the page. I don't know which MVVM framework you use but here's an example for MVVM Light.

Ilian
  • 4,840
  • 1
  • 31
  • 40
  • Im creating a MainMenuViewModel type variable in the ListViewModel for call a method inside MainMenuViewModel to change the value,but is not working. I described it in the post. – Paul Miranda Dec 13 '19 at 00:02
  • Fire up your debugger and figure out why it is null. See https://stackoverflow.com/q/4660142/324260. My best guess is you are not setting the `mmvm` property. – Ilian Dec 13 '19 at 00:04
  • Yeah, I'm not initializing the variable, just declaring it. How can I initialize it in a way it listen to the change from ListViewModel? – Paul Miranda Dec 13 '19 at 00:06