Navigation prism - previous and next views

I am developing a wpf application using prism and MVVM.

I have a main skin that has two areas, a menu area and a main area.

I am trying to implement in the menu area (which contains the MenuView) the BACK and FORWARD buttons, as in any browser: Let's say I have 5 views: View1, View2, View3, View4, View5.
When I launch the application, View1 is displayed in the main region. At the moment I want these buttons to be disabled. Now I choose to go to View3: View3 is displayed in the main region and then the Back btn becomes on (the front bit remains off). Then I go to View2 and it displays in the Main Region.

Now when I press Back btn view 3 is displayed in the main area and Forward btn is enabled. I click Forward and now View 2 is displayed and Forward btn is disabled.

I tried to use the navigation log as described in the following link:

But had no success because I did what they mentioned in the MenuViewModel, which is the only view that is displayed in the menu area for the entire app resource (and only for the main regions view). Therefore the OnNavigatedTo method is never called, because I will never navigate to the MenuView and this causes the navigationService to always be null.

The main thing is that I want these buttons to appear in the MenuView - the only view that appears in the menu area for the entire life of the application. Back and Forward buttons move between views in the Main Region - back and forth. Would eliminate your suggestions.


source to share

3 answers

This is how I solved it:

From the MenuViewModel, I have a link to RegionManager

, so I can access the main region and its navigation service:

var mainregion = _regionManager.Regions[RegionNames.mainregion];




you can use below code for previous or foreground

xml file

<Button Command="{Binding GoBackCommand}" Content="GoBack" />


ViewModel C #

private readonly IRegionManager _regionManager;
public ICommand GoBackCommand { get; set; }
public ClassName(IRegionManager regionManager)
    _regionManager = regionManager;
    GoBackCommand = new DelegateCommand(GoBack, CanGoBack);

private bool CanGoBack()
{  return _regionManager.Regions[RegionNames.MainRegion].NavigationService.Journal.CanGoBack;/*or CanGoForward */}

private void GoBack()
{ _regionManager.Regions[RegionNames.MainRegion].NavigationService.Journal.GoBack();/*GoForward()*/ }




var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
var moduleAView = new Uri("ModuleAContentView", UriKind.Relative);
regionManager.RequestNavigate("ContentRegion", moduleAView);


you can navigate between region above the code



All Articles