How to clear whole navigation history hosted by the WPF Frame control
Asked Answered
E

2

8

In a WPF application the Frame control is used to host/navigate pages. I'd like to clear the navigation history. There is NavigationService.RemoveBackEntry() method which can be used to clear the backward portion of the history. But what about the forward navigation history? How to clear this part? What is the best practice? Thank you in advance.

Elbertine answered 21/12, 2011 at 5:38 Comment(0)
K
14

Here's the code I used to clear a Frame's navigation history:

 public void ClearHistory()
 {
     if (!this.Frame.CanGoBack && !this.Frame.CanGoForward)
     {
         return;
     }

     var entry = this.Frame.RemoveBackEntry();
     while (entry != null)
     {
          entry = this.Frame.RemoveBackEntry();
     }

     this.Frame.Navigate(new PageFunction<string>() { RemoveFromJournal = true });
}
Katinakatine answered 14/5, 2012 at 2:49 Comment(3)
Note that this clears everything, but then you need to navigate somewhere, as this leaves you in nowhere-land.Polemic
This works great, but can someone explain it a bit what does it do ?Revareval
The navigation history is a stack so this code loops and takes the last entry off the stack until it is empty. Then to clear the forward navigation we tell the frame to navigate to an empty function and mark it with RemoveFromJournal so that it doesn't get added to the forward stack but replaces everything else on the forward stack. Leaving an empty history.Katinakatine
M
-1

I didn't try it, But you can try and navigate to the same page and than remove the backpage...

Mong answered 1/5, 2012 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.