0

I am making a WPF application for sales. What I want to do is to navigate to diffrent views while choosing whether I keep the navigation history (in case I want to keep a temporary instance of a transaction) or remove one or all history of navigation (in case I want to validate all temporary transactions) for example, I have a page transaction there is the case where I want to keep other instances of that page open temperarly or remove all instances if I am done with one or all transaction. I made a mainwindow that contain a frame . I want to navigate to a diffrent page inside the frame when a button is clicked while also choosing to keep or remove the navigation history. What I did so far was implement a window view model class and create a property (PageView) that is a enum that I can convert to the page view I want to show.

The pageview enum

public enum PageView
{
    Main=0,
    SecondPage=1
}

The converter from property PageView to the page I want to show

public class PageConverter : BaseCoverter<PageConverter>
{
    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch ((PageView)value)
        {
            
            default: Debugger.Break();
                return null;
            case PageView.Main:
                return new MainPage();
            case PageView.SecondPage:
                return new SecondoryPage();
        }
    }

    public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

}

Main window view model

class WindowVM:BaseViewModel
{
    private Window VWindow;
    private PageView _pageView= PageView.Main;
    public WindowVM(Window window)
    {
        VWindow = window; 
        SwitchView = new RelayCommand(_SwitchView, _CheckButton);
    }       
    public PageView CurrentPage
    {
        set
        {
            _pageView = value;
            OnPropertyChanged(); 
        }
        get { return _pageView; ; }
    }
    public ICommand SwitchView { get; set; }

    private void _SwitchView()
    {
        ((WindowVM)((MainWindow)Application.Current.MainWindow).DataContext).CurrentPage = PageView.SecondPage;
    }

    private bool _CheckButton(object parameter)
    {
        return true;
    }
}

What I have right now is a frame that create a new page each time the command SwitchView is used. What I want is to choose whether I want to be able to clear the history of the pages I navigated if i wanted to.

I found this solution but I don't want to use code behind to do that. I also found this solution but I really didn't know how to implement it.

Ak18 LUKA
  • 29
  • 5
  • You can use [NavigationService.RemoveBackEntry Method](https://learn.microsoft.com/en-us/dotnet/api/system.windows.navigation.navigationservice.removebackentry?redirectedfrom=MSDN&view=net-5.0#System_Windows_Navigation_NavigationService_RemoveBackEntry) to remove the most recent journal entry from back history.You can refer to this [answer](https://stackoverflow.com/a/6488787/9862794) to get using of it. – DasiyTian_1203 Dec 15 '20 at 08:35

1 Answers1

1

Just wrap the solution with NavigationService.RemoveBackEntry method in the behavior/attached property and apply it to your Frame in xaml.

<Frame local:FrameNoHistoryBehavior.Enable="True" />

And:

public static class FrameNoHistoryBehavior
{
    public static bool GetEnable(DependencyObject obj)
    {
        return (bool)obj.GetValue(EnableProperty);
    }

    public static void SetEnable(DependencyObject obj, bool value)
    {
        obj.SetValue(EnableProperty, value);
    }

    // Using a DependencyProperty as the backing store for Enable.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EnableProperty =
        DependencyProperty.RegisterAttached("Enable", 
            typeof(bool), 
            typeof(FrameNoHistoryBehavior), 
            new UIPropertyMetadata(false, OnEnablePropertyChanged));

    private static void OnEnablePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (!(d is Frame frame))
            throw new NotSupportedException($"This behavior supports only {nameof(Frame)}");

        if((bool)e.NewValue)
        {
            frame.Navigated += OnFrameNavigated;
        }
        else
        {
            frame.Navigated -= OnFrameNavigated;
        }

        void OnFrameNavigated(object sender, NavigationEventArgs e)
        {
            frame.NavigationService.RemoveBackEntry();
        }
    }
}