Remove a page from the back stack

Jan 27, 2014 at 8:15 AM
Edited Jan 27, 2014 at 8:15 AM
I need a menu option to "open" a file. Currently I have achieved this by doing the following:
    <mui:ModernWindow.TitleLinks>
        <mui:Link DisplayName="Open File" Source="/Open.xaml" />
    </mui:ModernWindow.TitleLinks>
Open.Xaml then overrides
        public async void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
Then it uses an openfiledialog to allow the user to select a file. I then navigate to the "view" page via:-
            ModernFrame frame = NavigationHelper.FindFrame(null, this);
            if (frame != null)
            {
                frame.Source = navigationUri;
            }
The issue I have is if the user clicks the back button they are asked to select a file again. Ideally what I would like to do is remove the "open.xaml" page from the back stack.

Any ideas?
Jan 27, 2014 at 4:11 PM
A workaround in case anyone needs to do this in the future.
        private void RemoveCurrentPageFromStack(ModernFrame frame)
        {
            //here be dragons. We need to modify the private field history from the above object
            //this is not something you would normally want to do, but there is no other way to remove the item afaik

            FieldInfo fi = typeof(ModernFrame).GetField("history", BindingFlags.NonPublic | BindingFlags.Instance);
            var history = fi.GetValue(frame) as Stack<Uri>;
            if (history != null)
            {
                history.Pop();               
            }
        }
Not ideal, but it works.
May 23, 2014 at 5:22 PM
how to clear all the history from the button?
Coordinator
Jun 5, 2014 at 6:57 PM
MUI doesn't support modification of the page history stack at this point in time. This is one of the most pressing issues with MUI at the moment. I'm working on solving this
Dec 29, 2014 at 10:29 PM
**bebe_ wrote:**
how to clear all the history from the button?
Taking the example from RossDargan you could create the following helper class methods:
private static Stack<Uri> GetStack(ModernFrame frame)
        {
            var fi = typeof(ModernFrame).GetField("history", BindingFlags.NonPublic | BindingFlags.Instance);
            if (fi == null) return null;
            return fi.GetValue(frame) as Stack<Uri>;
        }

public static void ClearHistory(this ModernFrame frame)
        {
            var history = GetStack(frame);
            if (history == null) return;
            history.Clear();
        }
Then you just need to get the frame using either the NavigationHelper class or using one of the navigation contracts, such as OnNavigatedFrom. For example
public void OnNavigatedFrom(NavigationEventArgs e)
        {
            e.Frame.ClearHistory();
        }