martints
martints

Reputation: 25

Prism navigation GoBack() using MVVM

I have an issue with creating a GoBack button (that works as intented :) ).

I am using MVVM and I have one region called ContentRegion. My MainWindow shows the GoBack Button and the region, that's it. Let's say I have ViewA and ViewB only. My basic navigation from ViewA to ViewB works fine. In MainWindow I set the region to ViewA at startup and I have my DelegateCommand for the GoBack button and the GoBack() and CanGoBack() methods:

public MainWindowViewModel(IRegionManager regionManager)
{
    _regionManager = regionManager;

    _regionManager.RegisterViewWithRegion("ContentRegion", typeof(AddCorpActView));

    NavigateCommand = new DelegateCommand(GoBack, CanGoBack);

}

private void GoBack()
{
    _regionManager.Regions["ContentRegion"].NavigationService.Journal.GoBack();
}

private bool CanGoBack()
{
    return true;
}

In ViewModelA and ViewModelB I have added the property:

private IRegionNavigationService _navigationService;

and

public void OnNavigatedTo(NavigationContext navigationContext)
{
    _navigationService = navigationContext.NavigationService;
}

When I use the GoBack-button to go from ViewB to ViewA I get no error, but nothing happens either.

Any input would be much appreciated.

Upvotes: 0

Views: 1159

Answers (1)

thatguy
thatguy

Reputation: 22119

The issue is this line, where you use view discovery to assign the initial view.

_regionManager.RegisterViewWithRegion("ContentRegion", typeof(AddCorpActView))

View discovery and view injection cannot be used in combination with the journal, see documentation.

The navigation journal can only be used for region-based navigation operations that are coordinated by the region navigation service. If you use view discovery or view injection to implement navigation within a region, the navigation journal will not be updated during navigation and cannot be used to navigate forward or backward within that region.

Instead use the navigation service only. Replace setting the initial view by view discovery with a call to the navigation service, e.g.:

_regionManager.RequestNavigate("ContentRegion", ...)

You could also move setting initial views with the navigation service to your module definition.

Upvotes: 2

Related Questions