CM 1.4: DisplayRootViewFor<MenuViewModel> works but DisplayRootView<MenuView> cannot find MenuViewModel

Topics: Bootstrappers & IoC, Conventions, Getting Started
Nov 27, 2012 at 11:53 AM
Edited Nov 27, 2012 at 12:51 PM

In the OnLaunched method of my bootstrapper, I try to load the MenuView by calling DisplayRootView<MenuView>(). But it seems it cannot find the matching view model MenuViewModel.

When I replace this call by DisplayRootViewFor<MenuViewModel>() the MenuView is loaded perfectly, but I want to use view first so I can use the navigation service.

What am I missing here?

I'm using the boostrapper from the HelloWinRT sample project.

Cheers

Nov 29, 2012 at 10:25 AM

Can you send me a sample of this not working to nigel.sampson@compiledexperience.com and I'll take a look.

Nov 30, 2012 at 1:00 AM

A common thing missing is the registration of the Frame control as the navigation service. In your App.xaml.cs you should have something like the following.

protected override void PrepareViewFirst(Frame rootFrame)
{
    container.RegisterNavigationService(rootFrame);
}

Nov 30, 2012 at 8:05 AM
Edited Nov 30, 2012 at 8:16 AM

As I was making a sample, I found the problem.

When adding a new XAML page to your project, the default DataContext is set like this

DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"

In some way, the ViewModel could not be located when the DataContext is already set? 

Thanks for you help Nigel!

Dec 2, 2012 at 4:46 AM

The Frame Adapter in CM (which handles the View first style approach) ensure it doesn't overwrite existing DataContexts, if one is set then it won't look for a matching view model.