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

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

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.


Nov 29, 2012 at 9:25 AM

Can you send me a sample of this not working to and I'll take a look.

Nov 30, 2012 at 12: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)

Nov 30, 2012 at 7:05 AM
Edited Nov 30, 2012 at 7: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 3: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.