Removing from, or opting out of registration, in the IRegionNavigationJournal
(This might be related to: https://github.com/PrismLibrary/Prism/issues/26 ?)
Currently I can find no mechanism for letting view models opt out of registration in a journal or for removing journal entries. The RecordNavigation method on the IRegionNavigationJournal interface can only look at the view's Uri and the navigation parameters, not the ViewModel instance. I have also looked at writing my own IRegionNavigationService, but this class too is unaware of the ViewModel instance.
I have the following use case in which such a scenario would be useful. Consider the following sequence of views: SomeView -> CreateItemView -> ItemView
Now from the ItemView I would like to 'GoBack' to the SomeView, skipping the CreateItemView.
It would be nice if there was a mechanism that lets ViewModels decide if they wish to be record in the journal or not, possibly via a parameter in NavigatedFrom?
If you are using ViewModel first, then you are navigating with VMs and not views in the journal. If you are using View first then you are navigating to Views and dealing with Views in the NavigationJournal.
To answer your question, you would simply call RequestNavigate("SomeView") in your ItemViewModel. Then you will be navigated to the current instance of the "SomeView". If you want to navigate to a new instance of "SomeView", then you would return false in INavigationAware.IsNavigationTarget.
Whoops, didn't mean to close, just meant to comment :0)
I'm not sure I understand. What I mean is that in some case you would like to remove (not record) items from the navigation journal. Its not a matter of managing the life time of the underlying view models, but not showing screens that do not make sense in a GoBack() action.
For example, in my case, navigating back from ItemView to CreateItemView would mean the user being prompted to create another item, instead going back to the an over view of items. I cannot use requestNavigate("SomeView") from the ItemView because a user could've gotten there from multiple views.
(Hope this makes more sense :) ).
Oh, I understand now, thanks for the clarification. We will think about how we can enable this scenario.
Hey Brian,
I was wondering if you already have an idea how to enable this scenario? Can you maybe give me some pointers where I could best implement this? I'll share the code of course :)
Unfortunately, we haven't had time to look into this.
I have a workaround to get the behavior you want using navigation identifiers. Every time you navigate to a view you provide a Guid to identify that request. When the journal browses back to a view that uses these ids it will pass in the same Guid of the original request. The destination view will then be able to decide whether the journal should roll back one more or not.
Example code is listed below. There are some caveats though:
- Every view that navigates to the view that needs to skip the journal needs to add a navigation id.
- You need to make sure that you don't mix up the navigation id with the decision making in
IsNavigationTarget. - Calling
Journal.GoBack()directly inOnNavigatedTowill result in a StackOverflow, that is why I call in asynchronously using the Dispatcher.
Navigation example of passing a navigation id:
var navParms = new NavigationParameters().AddNavigationId();
navigationService.RequestNavigation("Page", navParms);
OnNavigatedTo example of a view that should be skipped when the journal browses back:
var navId = navigationContext.Parameters.GetNavigationId();
if (this.prevNavId == Guid.Empty || this.prevNavId != navId)
{
this.prevNavId = navId;
.. take action ..
}
else
{
Dispatcher.CurrentDispatcher.BeginInvoke(() => navigationService.Journal.GoBack());
}
Helper extension methods:
public static NavigationParameters AddNavigationId(this NavigationParameters navigationParameters, Guid? navigationId = null)
{
navigationParameters.Add(SharedNavigationParameters.NavigationId, navigationId.HasValue ? navigationId : Guid.NewGuid());
return navigationParameters;
}
public static Guid GetNavigationId(this NavigationParameters navigationParameters)
{
var id = navigationParameters[SharedNavigationParameters.NavigationId];
return (Guid)id;
}
Although that is not a completely satisfactory work-around, due to the caveats you mentioned, it will do the job. Would be great if we have some more control over the Journal so that we do not have to use this.
Thanks for the help!
So I stared looking into this, and was thinking of the best way to accomplish this. I think simply adding some methods on the NavigationJournal would be the best, and least disruptive, approach. Something like:
Remove(string Uri, NavigationParameters parameters = null) RemoveAt(int index) RemoveLast()
Would that work?
Keep in mind that the navigation framework depends on the URI, so we aren't dealing with object instances here. We will look up the view to remove using either the URI, or a combination of the URI and matching parameter set. Even this approach will require rewriting how the NavigationJournal functions by replacing the Stack<T> variables with a different data type.
Thoughts?
Hey Brian,
Sorry for getting back to you so much later.
I've been thinking about the implication of adding these methods to the navigation journal. They might solve the issue, but I';m not sure about the order of events.
I was thinking that a viewmodel should be able to remove itself from the journal in the OnNavigatedFrom method. But I'm not sure if the current view model is, at that moment, already present in the navigation journal.
The reason why I think it should be done there is because that is usually the last possible moment for something to change in the view model, so in most cases it will have all the information required to determine if it should be skipped when going back or not.
(In this case you would only need the RemoveLast() method so you would not need to rewite the backend of the NavigationJournal).
Good idea. I am trying to get all the docs in order, and then I will start looking at larger features like this.
Luckily for us there is one area in which a new journal entry is made:
https://github.com/PrismLibrary/Prism/blob/master/Source/Wpf/Prism.Wpf/Regions/RegionNavigationService.cs#L269
Well, technically 3, but the other two are in the RegionNavigationJournal and they are exsiting entries, so as long as we prevent the entry from being made in the RegionNavigationService in the first place, it will never show up in the journal and we don't have to worry about it. So we have a couple of options that I can think of off the top of my head:
- Use a hard coded navigation parameter that is checked. Is this intuitive, error prone?
- Use an attribute. I don't like the idea of using reflection for every single navigation call.
- Use an interface. This is faster than an attribute and more explicit.
On thing I am worried about implementing this feature is that is opens up the developer to be confused and application bugs. Let's say we navigated like this:
ViewA -> ViewB -> ViewC
Now ViewC is marked to not be included in the journal. Now in ViewC I call GoBack(). What view did I just navigate to? Well we will go to ViewA, because the current entry in the journal is ViewB since ViewC did not get added. I can see this being a problem.
What are your thoughts?
Just thinking here, but I am wondering if adding an optional parameter on GoBack that would allow you to skip pages altogether. So basically all pages get added to thr backstack as expected, but I say GoBack(2) meaning go back 2 pages without ever actually navigating to the 1st one. So, given this scenario:
ViewA -> ViewB -> ViewC -> ViewD
I call GoBack(2), and then this is what my stack looks like now:
ViewA -> ViewB
But I never actually navigated ViewC, or maybe we do still navigate to VIewC, but you won't wind up there when the navigation is done. What do you think about that?
EDIT: actually, that's probably not a good idea. Might as well call GoBack twice to get the same result.
Sorry for the late reply. I was under the weather.
How about always entering a view in the navigation journal before 'OnNavigatedTo' (I think this is roughly where it happens now). This means that GoBack() will work as expected (from C to B). Then optionally removing the view from the navigation stack after 'OnNavigatedFrom'. I like the idea of an interface for this. We already have 'INavigationAware' and 'IKeepAlive', etc... We could add 'IJournalAware' or something like that with 'bool PersistInHistory()'.
I don't think using GoBack(2) or calling GoBack() multiple times is a good idea. It puts the responsibility of knowing if a view should be skipped at the wrong view. For example.
A->B->C->D
Say C is an optional 'log in' form to access a secure part (D) of the application. After having logged in once you will be logged in for the entire session. C knows this. However D is just another part of the application. Having to do GoBack(2) in D requires D to know that this time the user just logged in.
We can't add the entry before OnNavigatedTo in GoBack/GoForward because it relies on the navigation callback that is invoked after OnNavigatedTo in order to add an entry.
@rot-t I'm also in favour of an IJournalAware interface that you describe. That was where I searched for this feature after discovering i needed it. However, the view is deliberately separated from the journal entry. This allow us to use IsNavigation target to change the handling view at any point. To coordinate the 'skip' flag between views I would end up crowbarring something into NavigationParameters. Is that appropriate? It feels good to me.
@brianlagunas @roy-t @Gusdor @wldevries
Since I am currently deep involved in WPF region navigation, here is my take on this issue:
Define the interface (as suggested above)
public interface IJournalAware
{
bool PersistInHistory();
}
which may be implemented by either View or View Model.
Modify the RegionNavigationJournal 's method RecordNavigation to take another parameter
void RecordNavigation(IRegionNavigationJournalEntry entry, bool savePrevious = true);
When savePrevious parameter is false, the method will set RegionNavigationJournal.CurrentEntry to entry, but will not push previous value of the CurrentEntry onto back stack.
The RegionNavigationService.RequestNavigate method will query the currently active view for implementation of IJournalAware (as it currently does for IConfirmNavigationRequest) and use the value obtained by PersistInHistory as a parameter to RecordNavigation.
The only issue here is that, generally speaking, there may be several currently active views. How to handle this?
If you like it and still need it I may prepare a PR.
BTW, this can be done without modifications to the library: create custom implementations of IRegionNavigationService and IRegionNavigationJournal implementing the above and register them in the container instead of default ones.
You can have a PR ready, but it will be awhile before this will be reviewed.
@brianlagunas OK let's wait for the response from topic starter and other interested parties.
Callbacks or awaitables implementation or both?