How to find WPF page in navigation history to prevent it from being opened again


Gary Kindel

How can I determine if a WPF page is still in the WPF framework's navigation history before navigating back to that page?

overview:

My WPF application uses a single MainWindow with two frames, one for menu layout and another for displaying content. I'm using the MVVM pattern to display content as WPF pages as views with as little code behind each view/page as possible.

The content frame (shown in red) has NavigationUI visible.enter image description here

XAML:

enter image description here

Here is typical code to create a WPF page and display it in the main window via a static helper class:

public static void ShowPeriodicTable()
{
  var page = new Views.PeriodicTable();
  (Application.Current.MainWindow as MainWindow).ContentArea.Navigate(page);
}

This code loads the same page again and again, even though it's already loaded.

I made the following changes to detect if the current page is displayed in the MainWindow.ContentArea(frame) before creating and navigating to the page.

public static void ShowPeriodicTable()
    {                   
        var currentPage = ((DRC_SQLITE.MainWindow)Application.Current.MainWindow).ContentArea.Content;
        if (currentPage == null || (currentPage != null && currentPage.GetType().Name != "PeriodicTable"))
        {
            var page = new Views.PeriodicTable();
            (Application.Current.MainWindow as MainWindow).ContentArea.Navigate(page);
        }
    }

question:

How can I detect if the page to open exists in the navigation history, but is not the current page in the frame. In the image below, the settingPage is opened twice. I would like to eliminate this to better manage the memory usage of the application.

enter image description here

Andy

Pages are rarely used in commercial applications. As far as I understand they are indeed for xbap (wpf in browser). It is more common to host user controls in contentcontrols. In this case, there are two common options

1) Look first. Holds a dictionary of user controls typed by type. https://gallery.technet.microsoft.com/WPF-Navigation-Basic-Sample-11f10c74

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Navigate(typeof(HomeView));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)e.OriginalSource;
            Navigate(btn.Tag as Type);
        }

        private void Navigate (Type viewType)
        {
            UserControl uc;
            if (Views.ContainsKey(viewType))
            {
                uc = Views[viewType];
            }
            else
            {
                uc = (UserControl)Activator.CreateInstance(viewType);
                Views[viewType] = uc;
            }
            NavigationParent.Content = uc;
        }
        private Dictionary<Type, UserControl> Views = new Dictionary<Type, UserControl>();
    }

Proponents point out that navigation can be said to be a view responsibility.

2) First use Viewmodel.

Bind the content of the contentcontrol to a property in your mainwindowviewmodel. Switch it to the view model of each view, and switch it to the user control. You can then control the retention state by keeping a reference to the view model for each view.

If you really want to stick to what you have, then Frame.BackStack is a collection of entries in a journal. You can iterate over it and check the type of each object. I think it's actually a reference to the page. I've never seen this approach used in a commercial application.

Related


How to prevent programs from being opened by Python?

Wyren I would like to try to prevent programs from running with Python. For example, notepad.exe. My thoughts are as follows, but does this work? import os i = 0 while i < 1: os.system('taskkill /f /im notepad.exe') mshsem Your solution will work, but

How to prevent PDF files from being opened?

Jim Del I have an employee who reads books in PDF format while at work. When he sensed I was coming, he just closed the file. I really can't take drastic action against him (i.e. fire him) because the role he will be in will be hard to replace. I tried uninsta

How to prevent PDF files from being opened?

Jim Del I have an employee who reads books in PDF format while at work. When he sensed I was coming, he just closed the file. I really can't take drastic action against him (i.e. fire him) because the role he will be in will be hard to replace. I tried uninsta

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent commands from being deleted from history?

username I've noticed that when I use the arrow keys UPand traverse the command DOWN, if I edit the command afterwards, it will edit it in the history, I can even delete such a command, but I don't want it to have this effect commands in the history, so I don'

How to prevent commands from being deleted from history?

username I've noticed that when I use the arrow keys UPand traverse the command DOWN, if I edit the command afterwards, it will edit it in the history, I can even delete such a command, but I don't want it to have this effect commands in the history, so I don'

How to prevent commands in zshell from being saved to history?

Neil In Bash, I know putting a space before a command to prevent it from being kept in the history, what is the zshell equivalent? Darren Hall Use the HIST_IGNORE_SPACE option. setopt HIST_IGNORE_SPACE men shopping HIST_IGNORE_SPACE Remove the command line fr

How to prevent commands in zshell from being saved to history?

Neil In Bash, I know putting a space before a command to prevent it from being kept in the history, what is the zshell equivalent? Darren Hall Use the HIST_IGNORE_SPACE option. setopt HIST_IGNORE_SPACE men shopping HIST_IGNORE_SPACE Remove the command line fr

Prevent form from being opened multiple times

MKX2015 Since I'm new to C#, I think this question is a bit silly, but until now I haven't found a solution that fits the problem I'm having. I have two forms. In Form1, I click a button and a Form2 pops up, from which I can see the GridView. What I do is: sel

Prevent form from being opened multiple times

MKX2015 Since I'm new to C#, I think this question is a bit silly, but until now I haven't found a solution that fits the problem I'm having. I have two forms. In Form1, I click a button and a Form2 pops up, from which I can see the GridView. What I do is: sel

Prevent dpm from being enabled again

Degil I have manjaro linux and I want to disable dpms permanently. I have xautolock which enables screen lock (i3lock) after a while, which also puts my monitor to sleep. I know I can "disable" DPMS, xset -dpmsbut after a reboot or during a suspend it will be

Prevent dpm from being enabled again

Degil I have manjaro linux and I want to disable dpms permanently. I have xautolock which enables screen lock (i3lock) after a while, which also puts my monitor to sleep. I know I can "disable" DPMS, xset -dpmsbut after a reboot or during a suspend it will be