Navigation GoBack does not Close View

Topics: Prism v4 - WPF 4
Dec 1, 2010 at 8:18 PM
Edited Dec 1, 2010 at 9:04 PM

VB.NET, WPF 4.0, Prism 4.0

I have a solution with a primary desktop project and and another module.  The main project's shell view has a TabControl which is my MainRegion.  I have a menu that opens views in the mainregion.  This is all working fine but I cannot get the view to close.  I register the View from my Module with Unity and it launches successfully when selecting the appropriate menu option.  My ViewModel implements INavigationAware and stores the NavigationJournal in a local variable on the NavigateTo event.  I then have a CloseCommand on my ViewModel which is bound to a button on my TabHeader.  When the button is clicked the corresponding ExceuteCloseCommand routine is called but the view never gets removed from the TabControl.  My ExecuteCloseCommand routine simply issues a _navigationjournal.GoBack similar to the various examples out there.  If I check the _navigationjournal.CanGoBack it is false and I'm not sure why.

What could I be missing?  I read through Chapter 8 of the guidance and poured through various examples but could not find any other interface that was required for me to implement to get this to work.

Edit:  I created an even simpler example and still can't get it to work.  The ItemView opens fine in the TabControl in my shell but I can't get it to close.


Public Class ShellViewModel

  Private _regionManager As IRegionManager
  Private _navigatecommand As ICommand

  Public Sub New(ByVal regionManager As IRegionManager)
    _regionManager = regionManager
  End Sub

  Public ReadOnly Property NavigateCommand As RelayCommand(Of String)
      If _navigatecommand Is Nothing Then
        _navigatecommand = New RelayCommand(Of String)(AddressOf NavigateTo)
      End If
      Return _navigatecommand
    End Get
  End Property

  Private Sub NavigateTo(ByVal param As String)
    _regionManager.RequestNavigate("MainRegion", "ItemView")
  End Sub

End Class


Public Class ItemViewModel
  Implements INavigationAware

  Private _closecommand As ICommand
  Private _navjournal As IRegionNavigationJournal

  Public ReadOnly Property CloseCommand As ICommand
      If _closecommand Is Nothing Then
        _closecommand = New RelayCommand(AddressOf ExecuteClose)
      End If
      Return _closecommand
    End Get
  End Property

  Public Property DisplayName As String
      Return "Item View"
    End Get
    Set(ByVal value As String)
    End Set
  End Property

  Private Sub ExecuteClose()
    If _navjournal.CanGoBack Then
      MessageBox.Show("Cant do it!!")
    End If
  End Sub

  Public Function IsNavigationTarget(ByVal navigationContext As Microsoft.Practices.Prism.Regions.NavigationContext) As Boolean Implements Microsoft.Practices.Prism.Regions.INavigationAware.IsNavigationTarget
    Return False
  End Function

  Public Sub OnNavigatedFrom(ByVal navigationContext As Microsoft.Practices.Prism.Regions.NavigationContext) Implements Microsoft.Practices.Prism.Regions.INavigationAware.OnNavigatedFrom

  End Sub

  Public Sub OnNavigatedTo(ByVal navigationContext As Microsoft.Practices.Prism.Regions.NavigationContext) Implements Microsoft.Practices.Prism.Regions.INavigationAware.OnNavigatedTo
    If _navjournal Is Nothing Then
      _navjournal = navigationContext.NavigationService.Journal
    End If

  End Sub
End Class


Dec 1, 2010 at 9:29 PM


Based on my understanding when your application calls the GoBack method in the RegionNavigationJournal class, it does not navigate to the most recent entry in the back navigation history. Please, take into account that it does nothing if no entry exists in back navigation.

That said, you could check your code to see whether it is calling the Clear method in the RegionNavigationJournal class, since it clears your journal.

If you continue experiencing this issue, could you please provide a repro sample application?

Fernando Antivero

Dec 2, 2010 at 12:42 PM

OK, not sure what I changed to get this to work but now I have my CanGoBack showing True (most of the time).  I was not calling Clear anywhere, it just seemed to loose the history sometimes.  Is there any way to look at the URI that GoBack would go to?  Here is the reason for my request.  Lets say I have two views and they're being hosted in a TabControl with an "X" button to close the tab.  If I navigate to View1, then View2, then click the X button on View1 (which does NOT navigate to View1), the GoBack method wants to go back to View1 so the end results is View1 is closed and then re-opened.  So I would need to check and do something like: if jrnl.GoBackURI<>myURI then jrnl.GoBack.  My other thought was to manually add a journal.RecordNavigation(_currenturi) so the call gets pushed down the stack.  Maybe the Journal navigation just isn't meant for Tab based applications.


Dec 3, 2010 at 3:23 PM
Edited Dec 7, 2010 at 5:00 PM


I'm glad that you solved your problem regarding the GoBack method.

As you've mentioned, Journal navigation isn't meant for your scenario. The RegionNavigationJournal class does not expose a property that indicates the URI that the GoBack method navigates to. You could check the IRegionMemberLifetime.KeepAlive property, which might be useful to your scenario. You might find this chapter from the Prism v4 documentation, and this article from Karl Shifflett useful. The working of the aforementioned property, among other things, is explained there.

I hope you find this helpful.

Guido Leandro Maliandi