Unity and RegionNavigationJournal: CanGoBack returns false

Topics: Prism v4 - Silverlight 4
Dec 12, 2010 at 10:59 AM

My application has the same functionality as ViewSwitchingNavigation in QuickStarts but with a Unity container. I have the main content region with items and ShowDetails button. When I click ShowDetails and navigate to ShowDetailsView which implements INavigationAware, RegionNavigationJournal CanGoBack property is false. So I can't go back from my ShowDetailsView.

What am I doing wrong?

Sample code looks like this:


public class MainViewModel
{
    ...

    private void ExecuteCommandShowDetails(string parameter)
    {
        var builder = new StringBuilder();
        builder.Append("ShowDetailsView");
            
        if (parameter != null)
        {
            var query = new UriQuery();
            query.Add("Id", parameter);
            builder.Append(query);    
        }

        _regionManager.RequestNavigate(RegionNames.MainContentRegion, new Uri(builder.ToString(), UriKind.Relative));
    }
}

public class ShowDetailsViewModel : INavigationAware
{
    ...

    private IRegionNavigationJournal _navigationJournal;

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
        // In this place Journal.CanGoBack is false and Journal.CanGoForward is false
        _navigationJournal = navigationContext.NavigationService.Journal;
    }

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {


    }
}


Developer
Dec 13, 2010 at 9:49 PM

Hi,

There are no known issues with the Navigation Journal using Unity as the Dependency Injection container. It could be useful if you could provide us with a repro sample of your scenario, so that we can help you solve your issue.

Thanks.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Dec 14, 2010 at 11:16 AM
Edited Dec 14, 2010 at 12:43 PM

Hi,

this is repro sample with this issue: http://www.mediafire.com/?51q84fugdjdzf08

Developer
Dec 14, 2010 at 3:53 PM

Hi Johnny,

Thank you for providing the repro sample. The issue you're experiencing is because you're registering your first view (the SearchAnimalsView) using View Discovery (that is, using the RegionManager.RegisterViewWithRegion method).

This is explained on the following note from the Navigation Chapter of the Prism MSDN 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."

In order to fix this behavior, I've replaced this line in your ModuleInit class:

_regionManager.RegisterViewWithRegion(RegionNames.MainContentRegion, () => _container.Resolve<ViewSearchAnimals>());

by the following lines:

_container.RegisterType<Object, ViewSearchAnimals>("ViewSearchAnimals");
_regionManager.RequestNavigate(RegionNames.MainContentRegion, "ViewSearchAnimals");

That way, when your first view (the SearchAnimalsView) is loaded  on application start-up (by being navigated to), a RegionNavigationJournalEntry is recorded in the RegionNavigationJournal, which indicates that it is the current entry in your navigation journal. So, when you navigate to the second view (the AnimalProfileView), the RecordNavigation method will push the current entry to the backStack, thus making the CanGoBack property return true.

Below you can see the RegionNavigationJournal.RecordNavigation method and RegionNavigationJournal.CanGoBack property to illustrate this:

        public void RecordNavigation(IRegionNavigationJournalEntry entry)
{
if (!this.isNavigatingInternal)
{
if (this.CurrentEntry != null)
{
this.backStack.Push(this.CurrentEntry);
}

this.forwardStack.Clear();
this.CurrentEntry = entry;
}
}


        public bool CanGoBack
{
get
{
return this.backStack.Count > 0;
}
}

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Dec 14, 2010 at 5:19 PM

It was very helpful to me. Thank you very much for detailed explanation.

Feb 28, 2011 at 1:04 PM
Edited Feb 28, 2011 at 1:36 PM

Dear Guido Leandro,

Great indeed !!

Waleed