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?
            Asked
            
        
        
            Active
            
        
            Viewed 1,050 times
        
    -1
            
            
         
    
    
        Paul Miranda
        
- 738
- 17
- 39
- 
                    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 Answers
0
            
            
        A couple of options:
- If - MainViewModeland- ListViewModelhave a direct relationship, you can just pass a reference to- MainViewModelwhen constructing- ListViewModel. Then just call- MainViewModel'sproperty directly from- ListViewModel.
- If they don't have a direct relationship, you can use the Messenger pattern. Basically, - MainViewModelwill register to a type of message (e.g- ChangePageMessage) and then- ListViewModelwill 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
        
- 5,113
- 1
- 32
- 41
- 
                    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