I found this to be a common problem in Silverlight applications that I write using the MVVM pattern. I use the NavigationHelper class to centralize the logic around navigation. It looks something like this:
public interface INavigationHelper { void Home(); void SomeOtherPage(); } public class NavigationHelper : INavigationHelper { private NavigationService _navSvc; public NavigationHelper(NavigationService navSvc) { _navSvc = navSvc; } public void Home() { _navSvc.Navigate(new Uri("/Home", UriKind.Relative)); } public void SomeOtherPage() { _navSvc.Navigate(new Uri("/SomeOtherPage", UriKind.Relative)); } }
Then I have a ViewModel property with the NavigationHelper property, which is set by the page when creating the ViewModel.
By the way, it seems like it would be easier for the NavigationHelper to be passed in the ViewModel constructor. But the presence of constructors that do not have default values ββfor the ViewModel complicates the work on a project in Blend, in my experience.
Rationalgeek
source share