Remove forward entry on Navigation service?

2019-06-22 00:14发布

问题:

How would one remove all the forward entries in a navigation service?

I tried this but it is crashing.

    while (NavigationService.CanGoForward) NavigationService.RemoveBackEntry();

I know "RemoveBackEntry()" seems odd but there is no RemoveForwardEntry() method.

Any ideas?

Thanks, Kohan

Edit 1: Im a little closer, i can access the forward stack, and even output each item in there but i can not seem to work out how to remove the entries. None of the properties or methods on _frame.ForwardStack or j give any insight into how to remove these entries.

        Window mainWindow = Application.Current.MainWindow;
        Frame _frame = (Frame)mainWindow.FindName("mainFrame");
        foreach (JournalEntry j in _frame.ForwardStack)
        {
            MessageBox.Show(j.Name);
        }

回答1:

I read into the wpf navigation a little more and if you can get to the instance of NavigationWindow for your application there is a property called ForwardStack that holds the list of forward navigation pages. You should be able to add or remove pages from there.

I have not tried this myself as I do not have a project to test this on right now so let me know if this works as I would like to try this myself in the future.

See msdn link for full member listing: http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationwindow_members.aspx



回答2:

Well, it's never too late for an answer !

The following piece of code will simply disable forward navigation:

    void Frame_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        bool b = e.NavigationMode == NavigationMode.Forward;
        if (b)
        {

            e.Cancel = true;
        }
    }

Currently it is for the Frame.Navigating event but it should apply for Application as well as NavigationWindow (did not test though).

EDIT:

Here is a Behavior for a Frame:

public class FrameNavigationBehavior : Behavior<Frame>
{
    public static readonly DependencyProperty CanGoForwardProperty = DependencyProperty.Register(
        "CanGoForward", typeof (bool), typeof (FrameNavigationBehavior), new PropertyMetadata(true));

    public static readonly DependencyProperty CanGoBackwardProperty = DependencyProperty.Register(
        "CanGoBackward", typeof (bool), typeof (FrameNavigationBehavior), new PropertyMetadata(true));

    public static readonly DependencyProperty CanRefreshProperty = DependencyProperty.Register(
        "CanRefresh", typeof (bool), typeof (FrameNavigationBehavior), new PropertyMetadata(true));

    public bool CanGoForward
    {
        get { return (bool) GetValue(CanGoForwardProperty); }
        set { SetValue(CanGoForwardProperty, value); }
    }

    public bool CanGoBackward
    {
        get { return (bool) GetValue(CanGoBackwardProperty); }
        set { SetValue(CanGoBackwardProperty, value); }
    }

    public bool CanRefresh
    {
        get { return (bool) GetValue(CanRefreshProperty); }
        set { SetValue(CanRefreshProperty, value); }
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Navigating += AssociatedObject_Navigating;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.Navigating -= AssociatedObject_Navigating;
    }

    private void AssociatedObject_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        NavigationMode navigationMode = e.NavigationMode;
        switch (navigationMode)
        {
            case NavigationMode.New:
                break;
            case NavigationMode.Back:
                if (!CanGoBackward)
                {
                    e.Cancel = true;
                }
                break;
            case NavigationMode.Forward:
                if (!CanGoForward)
                {
                    e.Cancel = true;
                }
                break;
            case NavigationMode.Refresh:
                if (!CanRefresh)
                {
                    e.Cancel = true;
                }
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
}


回答3:

If you renavigate to the current page after you go backwards you should lose all of the forward data just like you would in a web browser or in windows explorer.

If you don't want the refresh to show in the back list you can then remove the last entry from the back list.