The NavigationService prism gets the previous view name

I am currently implementing a screen showing that the module does not exist or is still in development.

enter image description here

The back button has the following code:

 regionNavigationService.Journal.GoBack();

      

This works as expected. But the user doesn't leave the screen Home

. So I need to access the view name from the last entry in the navigation log.

Example: User exits the settings screen => The text should display "Back to settings screen"

+3


source to share


1 answer


Assuming the view name you are looking for is when you make a new Uri ("Main", UriKind.Relative) that you want the word Main to be in the main view.

The forward and backward stacks in RegionNavigationJournal are private. You can use reflection to access it.

var journal = regionNavigationService.Journal as RegionNavigationJournal;
if (journal != null)
{
    var stack =
        (Stack<IRegionNavigationJournalEntry>)
        typeof (RegionNavigationJournal).GetField("backStack",
                                                  BindingFlags.NonPublic | BindingFlags.Instance)
                                        .GetValue(journal);

    var name = stack.Peek().Uri.OriginalString;
}

      

Or a better way is to implement your own IRegionNavigationJournal, which is a wrapper around it. This is Unity use to have the constructor inject the default RegionNavigationJournal, if you are using MEF you may need to cast ImportingConstructorAttribute on it.

public class RegionNavigationJournalWrapper : IRegionNavigationJournal
{

    private readonly IRegionNavigationJournal _regionNavigationJournal;
    private readonly Stack<Uri> _backStack = new Stack<Uri>();

    // Constructor inject prism default RegionNavigationJournal to wrap
    public RegionNavigationJournalWrapper(RegionNavigationJournal regionNavigationJournal)
    {
        _regionNavigationJournal = regionNavigationJournal;
    }

    public string PreviousViewName
    {
        get
        {
            if (_backStack.Count > 0)
            {
                return _backStack.Peek().OriginalString;
            }
            return String.Empty;
        }
    }

    public bool CanGoBack
    {
        get { return _regionNavigationJournal.CanGoBack; }
    }

    public bool CanGoForward
    {
        get { return _regionNavigationJournal.CanGoForward; }
    }

    public void Clear()
    {
        _backStack.Clear();
        _regionNavigationJournal.Clear();
    }

    public IRegionNavigationJournalEntry CurrentEntry
    {
        get { return _regionNavigationJournal.CurrentEntry; }
    }

    public void GoBack()
    {
        // Save current entry
        var currentEntry = CurrentEntry;
        // try and go back
        _regionNavigationJournal.GoBack();
        // if currententry isn't equal to previous entry then we moved back
        if (CurrentEntry != currentEntry)
        {
            _backStack.Pop();
        }
    }

    public void GoForward()
    {
        // Save current entry
        var currentEntry = CurrentEntry;
        // try and go forward
        _regionNavigationJournal.GoForward();
        // if currententry isn't equal to previous entry then we moved forward
        if (currentEntry != null && CurrentEntry != currentEntry)
        {
            _backStack.Push(currentEntry.Uri);
        }
    }

    public INavigateAsync NavigationTarget
    {
        get { return _regionNavigationJournal.NavigationTarget; }
        set { _regionNavigationJournal.NavigationTarget = value; }
    }

    public void RecordNavigation(IRegionNavigationJournalEntry entry)
    {
        var currentEntry = CurrentEntry;
        _regionNavigationJournal.RecordNavigation(entry);
        // if currententry isn't equal to previous entry then we moved forward
        if (currentEntry != null && CurrentEntry == entry)
        {
            _backStack.Push(currentEntry.Uri);
        }
    }
}

      



If you are using unity in your Prism Bootstrapper, you will need to replace the default IRegionNavigationJournal journal registration

protected override void ConfigureContainer()
{
    this.RegisterTypeIfMissing(typeof(IRegionNavigationJournal), typeof(RegionNavigationJournalWrapper), false);

    base.ConfigureContainer();
}

      

If you are using MEF you will need to put ExportAttribute on top of RegionNavigationJournalWrapper

[Export(typeof(IRegionNavigationJournal))]

      

You can see http://msdn.microsoft.com/en-us/library/gg430866%28v=pandp.40%29.aspx for more information on replacing your default implementation with your own. Once you have the wrapper, you will still need to use it as a RegionNavigationJournalWrapper to access the PreviousViewName, so it is still not ideal or create an interface that RegionNavigationJournalWrapper also implements to pass it in order to access the PreviousViewName view

+11


source







All Articles