A bit late to this issue, but it is relevant and, I hope, will benefit someone. I had to create an SL4 application with MvvmLight and wanted to use a wrapper for the navigation service that was mock-up and could be introduced into the ViewModel. I found a good starting point here: Laurent Bugnion SL4 trial code examples from Mix11, which includes a demo of a navigation service: Deep Dive MVVM Mix11
Here are the main parts for implementing a breadboard navigation service that can be used with Silverlight 4. The key problem is getting a link to the main navigation frame that will be used in the custom NavigationService class.
1) In MainPage.xaml, the navigation frame is given a unique name, for this example there will be a ContentFrame:
<navigation:Frame x:Name="ContentFrame" Style="{StaticResource ContentFrameStyle}" Source="/Home" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed"> </navigation:Frame>
2) In MainPage.xaml.cs, the navigation frame is displayed as a property:
public Frame NavigationFrame { get { return ContentFrame; } }
3) The navigation service class implements the INavigationService interface and relies on the NavigationFrame property MainPage.xaml.cs to get a link to the navigation frame:
public interface INavigationService { event NavigatingCancelEventHandler Navigating; void NavigateTo(Uri uri); void GoBack(); } public class NavigationService : INavigationService { private Frame _mainFrame; public event NavigatingCancelEventHandler Navigating; public void NavigateTo(Uri pageUri) { if (EnsureMainFrame()) _mainFrame.Navigate(pageUri); } public void GoBack() { if (EnsureMainFrame() && _mainFrame.CanGoBack) _mainFrame.GoBack(); } private bool EnsureMainFrame() { if (_mainFrame != null) return true; var mainPage = (Application.Current.RootVisual as MainPage); if (mainPage != null) {
Bgrva source share