Page-Navigation within ViewModels

15. February 2011

This blogpost is more like a little hint or tip, if you want to develop your application with the MVVM – design pattern. Within Windows Phone 7 apps you can easily navigate through PhoneApplicationPages in the code-behind of your views with the NavigationService – Class.

But what if you handle some logic/calculation within a ViewModel and you want to navigate to another View on the basis of the result, you cannot access the NavigationService in this static context.

The solution for this is very simple! All you have to do is the following:

  • In the appropiate ViewModel, just create a property of type NavigationService, like so



  • using the NavService like you do in code-behind

  • and don`t forget to set the Property in the code-behind of the approriate view, so that the NavService is valid and to avoid a NullReferenceException 🙂 You can do this in the constructor or any other initialization method.

I hope, that this little tip is useful for someone.

Advertisements

One Response to “Page-Navigation within ViewModels”

  1. […] This post was mentioned on Twitter by Matt Stroshane and Ginny Caughey, cordellcp3. cordellcp3 said: Just blogged: Page-Navigation within ViewModels http://bit.ly/gVlnJA #wp7dev #silverlight […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: