0

I have a window, which contains a frame. The frame's source loads in as "HomePage.xaml".

I am converting everything over to MVVM, but struggling. Previously, I had this function in the code behind:

private void HiscoreStatsTile_Click(object sender, RoutedEventArgs e)
{
    NavigationService navService = NavigationService.GetNavigationService(this);
    ChartTest x = new ChartTest();
    navService.Navigate(x);
}

However, I can't quite get this to work as a command in my viewmodel. How should I get a hold of that frame containing the view, and navigate it to a new page?

pingu2k4
  • 956
  • 2
  • 15
  • 31

1 Answers1

0

You should not access your view or any UI controls in your viewmodels.

I prefer viewmodel first navigation described here: https://stackoverflow.com/a/28916120/475727 Things will be much easier then, believe me

Build in navigation service does not work very well in mvvm scenarios. Just write your own. It's couple lines of code. Basically you need Navigate method that takes viewmodel as parameter and CurrentPage property that represent viewmodrl of current page. In your main view you just bind frame or even better contentcontrol to currentpage property and use datatemplate selector or converter to create view for your currentpage view model

Community
  • 1
  • 1
Liero
  • 25,216
  • 29
  • 151
  • 297