Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prism navigation - previous and next view

Tags:

c#

mvvm

wpf

prism-4

I'm developing a wpf application using prism and MVVM.

I have a main shell, which has two regions: Menu region and Main region.

I'm trying to implement within the menu region (which contains the MenuView) a BACK and FORWARD buttons, exactly as we have in any browser: Let's say I have 5 views: View1, View2, View3, View4, View5.
When I launch the application, View1 is shown in the Main Region. For the current moment, I want those buttons to be disabled. Now, I choose to navigate to View3: View3 is shown in the Main Region, and then the Back btn becomes enabled (the forward btn is remained disabled). Then, I navigate to View2, and it is displayed in Main Region.

Now, when I click Back btn, View 3 is displayed in Main Region, and the Forward btn becomes enabled. I click Forward, and Now View 2 is displayed, and Forward btn becomes disabled.

I've tried using the Navigation Journal, as explained in the following link: http://msdn.microsoft.com/en-us/library/gg430861(v=pandp.40).aspx

But had no success, because I did what they mentioned in the MenuViewModel, which is the only view that is dispayed in the Menu region for the whole application life (and only Main Region switches views). That's why, OnNavigatedTo method is never called, cause I never nevigate to the MenuView, and this causes navigationService to always be null.

The main point is that I want these buttons in the MenuView - the only view displayed in the Menu Region for all the application life. And the Back and forward buttons navigate between views in the Main Region - back and forth. Would appreaciate your suggestions.

like image 271
DimaK Avatar asked Mar 18 '23 10:03

DimaK


2 Answers

This is how I solved it:

From the MenuViewModel, I have a reference to the RegionManager, so I can get access to the main region and it's navigation service:

var mainregion = _regionManager.Regions[RegionNames.mainregion];
mainregion.NavigationService.Journal.GoForward();
like image 149
DimaK Avatar answered Mar 23 '23 12:03

DimaK


you can use bottom code for previous or forward region

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()*/ }
like image 27
pejman Avatar answered Mar 23 '23 10:03

pejman