Jason James
Jason James

Reputation: 1092

Navigating to a new page from a viewmodel

I have used the WP7 DataBound app template for my program which utilises a form of MVVM. I would like to navigate from my mainpage to my settings page but since it is the VM that loads the data, which in turn determines if the settings pages needs to be loaded, I should like to call navigate code from the VM, not the view. However, NavigationService does not show the Navigate method in the VM, only in the view. Does anyone have any suggesions about how I can/should call the Navigate method in the VM?

Thanks,

Jason.

Upvotes: 0

Views: 611

Answers (2)

Claus Jørgensen
Claus Jørgensen

Reputation: 26347

Implement it as a static method in your App.xaml.cs, like this

public static void Navigate(Uri source)
{
    Dispatcher.BeginInvoke(() => (App.Current.RootVisual as PhoneApplicationFrame).Navigate(source));
}

That way you can simply call App.Navigate(uri) from anywhere.

You might also want to implement GoBack():

public static void GoBack()
{
    Dispatcher.BeginInvoke(() => (App.Current.RootVisual as PhoneApplicationFrame).GoBack());
}

Upvotes: 1

Random Dev
Random Dev

Reputation: 52280

i tend to use one of the following

  • publish a event in the VM, subscribe in the view
  • implement a global NavigationService (using the ServiceLocator pattern or DI), you can for example implement such a Interface in your application-class

Upvotes: 1

Related Questions