In my WPF application, I’m using a Frame for navigation between pages. The issue I’m encountering is that whenever I navigate to a page that is already open, a new instance of that page is added to the navigation history. This results in multiple instances of the same page being stored in the history, which I want to avoid.
This is what I want to avoid
The class that I use to navegate is this I tried to avoid it creating only one instance of the class but it dosn’t work for my problem:
using System.Windows;
public partial class MainPage : Window
{
public ClientWindow ClientsPage;
public InvoiceWindow DraftInvoicePage;
public InvoiceWindow IssuedInvoicePage;
public InvoiceWindow SentInvoicePage;
public MainPage()
{
InitializeComponent();
MainFrame.Navigate(new DefaultPage(this));
}
public void LoadClientsPage(object sender, RoutedEventArgs e)
{
if (ClientsPage == null)
{
ClientsPage = new ClientWindow();
}
MainFrame.Navigate(ClientsPage);
}
public void LoadDraftInvoicesPage(object sender, RoutedEventArgs e)
{
if (DraftInvoicePage == null)
{
DraftInvoicePage = new InvoiceWindow(InvoiceWindow.InvoiceType.Draft);
}
MainFrame.Navigate(DraftInvoicePage);
}
public void LoadIssuedInvoicesPage(object sender, RoutedEventArgs e)
{
if (IssuedInvoicePage == null)
{
IssuedInvoicePage = new InvoiceWindow(InvoiceWindow.InvoiceType.Issued);
}
MainFrame.Navigate(IssuedInvoicePage);
}
public void LoadSentInvoicesPage(object sender, RoutedEventArgs e)
{
if (SentInvoicePage == null)
{
SentInvoicePage = new InvoiceWindow(InvoiceWindow.InvoiceType.Sent);
}
MainFrame.Navigate(SentInvoicePage);
}
public void OpenConfiguration(object sender, RoutedEventArgs e)
{
ConfigurationWindow configWindow = new ConfigurationWindow();
configWindow.ShowDialog();
}
}
Oriol Tomàs Lara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2