Switching between [Page Class] pages in an application

Asked

Viewed 38 times

0

I’m looking up examples of browsing between pages in a desktop application. Let’s assume that the navigation is done from a Listbox always visible in Ui. Most examples do something like this to switch to a particular page:

private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    int index = myListBox.SelectedIndex;
    switch(index+1)
    {
        case 1:
            mainFrame.Content = new Page1();
            break;
        case 2:
            mainFrame.Content = new Page2();
            break;
        case 3:
            mainFrame.Content = new Page3();
            break;
    }
}

My question in this case is about memory management. Won’t this type of approach create an undetermined number of instances of the various invoked page? I ask this because, by activating the navigation bar, I find the different instances of the same class. Something like this won’t be right anymore?

private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    int index = myListBox.SelectedIndex;
    switch(index+1)
    {
        case 0: mainFrame.Content = p0;
            break;
        case 1:
            mainFrame.Content = p1;
            break;
        case 2:
            mainFrame.Content = p2;
            break;
        case 3:
            mainFrame.Content = p3;
            break;
    }
}
Page0 p0 = new Page0();
Page1 p1 = new Page1();
Page2 p2 = new Page2();
Page3 p3 = new Page3();

1 answer

0

Depends on your application:

If you have a system with few pages, load them initially, in the second option may be ideal;

But at first besides "clean" the page, it will be loaded on-demand, and for a system of many pages it would be best to load one instance at a time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.