Switching between pages [Page Class] in an application

0

I'm searching for examples of browsing between pages in a desktop application. Let's assume that navigation is done from a ListBox always visible in the 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 here is about memory management. This kind of approach will not create an undetermined number of instances of the multiple page invoked? I ask this because, by activating the navigation bar, I find the different instances of the same class. Is not something more correct?

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();
    
asked by anonymous 05.03.2017 / 13:13

1 answer

0

It depends on your application:

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

But in the first one besides "clearing" 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.

    
07.03.2017 / 15:22