Is there any way to call Navigate from ViewModel?

I have a Silverlight 4 project that displays a chart and some buttons that allow the user to change the date range of the chart. The date range can also be passed through the query string parameter - something like http: // myserver / MySilverlightPage / #? DateRange = OneMonth - and when the user clicks the button, I would update Url for example.

I understand that the way to do this is to call this.NavigationService.Navigate(new Uri(...)) , but as far as I can tell, this can only be done from the Silverlight page code. And since I use MVVM, all command processing happens in the ViewModel class. Is there a way to call Navigate or otherwise change the URL from the ViewModel?

To clarify, xaml includes the following Button :

 <Button Content="1 Month View" Command="{Binding OneMonthCommand}" /> 

And the ViewModel class contains the OneMonthCommand property:

 public ICommand OneMonthCommand { get; set; } 

When a button is clicked, my ICommand Execute implementation method starts. The question is, how can I change the url from this method?

+7
silverlight mvvm navigation
source share
3 answers

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.

+8
source share

If you just do regular navigation, you should use the usual HyperlinkButtons buttons. If you are trying to navigate in response to other events, then you can use messaging.

An alternative is that the View has passed the NavigationService class to your ViewModel, and if you use the base page and base view model, you can ensure that this happens to those who do not have the requirements of each view model, and knowing about the transfer happens.

+2
source share

Using MVVM does not preclude the use of hyperlink buttons if they perform the required task.

The problem, as you have discovered, with NavigationService.Navigate is that it requires knowledge of the page for its context.

I don’t think it’s considered too β€œevil” to insert your current look back into it to view the model when you set the datacontext to codebehind. In general, worse, because View knows too much about its ViewModel.

+1
source share

All Articles