Navigate from ViewModel

Jan 14, 2011 at 9:17 AM


I have two with with their ViewModels.

My application starts lets say with the View1. I do something there and I press a button which has a Command which is executed in the ViewModel1. Now a would like to navigate to my View2 after that button click in ViewModel1. Can I achieve this somehow?

Jan 14, 2011 at 9:50 AM

Well, you have a couple of options:

1. You can make use of the NavigationService static class - it allows you to induce navigate from just about anywhere. Further, you can give any navigation-container a name by using the NavigaitonHandler behavior, and using that name, you can specify to the NavigaitonService which container to use.

2. I tend to prefer this; basically make a ReverseCommand in your VM and execute it from your VM. For example, below when I call the WaitReverseCommand it would navigate the specified Url in the specified navigation container.

        <n:ReverseCommandTrigger ReverseCommand="{Binding WaitReverseComamnd}">
		<n:NavigateAction HandlerName="ShellHandler" Url="Pages/WaitScreen/" />

The benefit being, it keeps your View specific logic out of your VM - I consider Urls to be a View detail.