Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove forward entry on Navigation service?

Tags:

c#

navigation

wpf

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);
        }
like image 476
4imble Avatar asked Nov 06 '22 18:11

4imble


1 Answers

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();
        }
    }
}
like image 82
aybe Avatar answered Nov 09 '22 23:11

aybe