title: Navigation between Pages and Windows in WPF
body: <p>I have an issue that I am trying to resolve. I have two WPF windows. Those windows house the pages with content. My problem is not navigating page to page but rather window to window. here is an example of the code:</p>
  <p><code>LoginPortal Design</code></p>
  <pre><code> &lt;Grid&gt;
      &lt;Frame x:Name=&quot;MainFrame&quot; NavigationUIVisibility=&quot;Hidden&quot;/&gt;
  &lt;/Grid&gt;
  </code></pre>
  <p><code>LoginPortal Code</code></p>
  <pre><code> public LoginPortalMain()
      {
          InitializeComponent();
  
          MainFrame.Navigate(new Uri(&quot;/Pages/LoginPortal.xaml&quot;, UriKind.RelativeOrAbsolute));
  
       
      }
  </code></pre>
  <p>This is my Login portal that houses 4 buttons. Each button opens the second window which is houses several pages.</p>
  <pre><code>private void BtnSales_Click(object sender, RoutedEventArgs e)
      {
          var w = Application.Current.Windows[0];
          w.Hide();
          MainWindow mn = new MainWindow();
          mn.Content = new SalesPage();
          mn.Show();
      }
  </code></pre>
  <p>When this button is clicked it opens the second WPF Window which is where several pages are housed. There is no problem with navigation. The problem starts when I wire the Closing event from the MainWindow to go back to the LoginPortalMain</p>
  <pre><code>    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
      {
          LoginPortalMain lgm = Application.Current.MainWindow as LoginPortalMain;
          lgm.Show();
      }
  </code></pre>
  <p>It then goes back to the start again. Now my problem is if I click on the same button again. It just duplicates everything. Then on closing anything it just adds new windows. If I then exit out of any window, it throws an error in picture below. My question is, is there a way to either dispose of the navigation once the MainWindow is closed. So there is no duplicate windows once you close the mainWindow a second time. I have used navigation before on a single window but not on multiple windows.</p>
  <p>I may have to rethink this whole structure and use custom UserControls and figure out the animation sequence from Pages to UserControls. However, any help would be greatly appreciated.</p>
  <p><a href="https://i.stack.imgur.com/TuE7E.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><a href="https://i.stack.imgur.com/A1kBH.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><a href="https://i.stack.imgur.com/PS2HA.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><a href="https://i.stack.imgur.com/Rl5ra.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><a href="https://i.stack.imgur.com/buTfQ.png" rel="nofollow noreferrer">enter image description here</a></p>