Is this intended behavior?

Jun 24, 2014 at 1:53 AM
If I construct my viewmodel as a page resource in xaml my viewmodels OnNavigatedTo method is never called. If I use the ViewModelLocator and set it to AutoWire=true, the the method is called. Is this the intended behavior of the Library? If so could someone please explain to me why that is?

Jun 24, 2014 at 8:24 AM
The viewmodel has to be instantiated.
If you set AutoWire=true, the VM will be automatically instantiated in the app.xaml.cs class.
The idea behind this behaviour is convention over configuration.

If you don't use AutoWire=true, you have to instantiate the VM manually and set the DataContext in the codebehind or the XAML code of the View itself.
Jun 24, 2014 at 10:16 AM
Pardon me if I am wrong, but I thought that this:

   <data:MainPageViewModel x:Key = "vm"></data:MainPageViewModel>

indeed instantiated the viewmodel when the xaml is parsed.
Jun 24, 2014 at 10:53 AM
That just lets the page know about the resource. You need to assign it to the DataContext for it to be used. To confirm that you are indeed using the viewmodel just put a breakpoint in the constructor to see when it is created.
Jun 24, 2014 at 10:55 AM
I do have the DataContext property of my root grid set to the static resource and the viewmodel is constructed.
Jun 24, 2014 at 10:19 PM
I think I figured it out. If I set the pages data context in the code behind to the viewmodel then everything works as expected. That leaves me with another question or two though.


Do I still need to construct the viewmodel as a static resource like this:
       <data:MainPageViewModel x:Key="vm"></data:MainPageViewModel>

Is there any other way to set the pages data context to the viewmodel without using AutoWire = true?

Aug 31, 2014 at 9:27 PM
Pages 52 through 55 of the PDF documentation present some other techniques to bind the ViewModel to the View.
Perhaps that puts you on the right track?