Page 1 of 1
Screens are constructed every time they are shown?
Posted: Tue Aug 28, 2012 2:07 pm
by charliehbeijer
I gather that screen objects are destroyed when the screen isn't showing, right?
I have a listbox on a screen, which contains information generated by the program. When I switch to another screen, the listbox is destroyed, apparently. So I have to fill the listbox every time the screen is shown. It takes a noticible amount of time.
Is there any way to keep the listbox, or the whole screen, in memory when the screen isn't being shown?
Re: Screens are constructed every time they are shown?
Posted: Tue Aug 28, 2012 3:31 pm
by mark.monroe
How are you changing screens? Are you closing the screen with the list box? Or are you creating a new screen using the show method?
That screen with the list box will only be destroyed when the C# garbage collector decides it needs more memory, or you close it. Otherwise that screen is still there. It is just covered up by the screen that gets created when you call show screen. If you were to close all the screens on top of that particular screen, then it should still have it's list box full.
Re: Screens are constructed every time they are shown?
Posted: Tue Aug 28, 2012 5:20 pm
by charliehbeijer
I have a number of screens. I have buttons, with their click actions bound to "ShowScreen" in order to switch between screens.
So I'm on screen TestHistoryScreen, and I want to stuff the list box with some initial data. I can't write a constructor, because it says there is already a constructor. I can't access the screen object from elsewhere in the project, all I can get is Globals.TestHistoryScreen, which is apparently an interface, not the actual screen object.
So I do this:
Code: Select all
public partial class TestHistoryScreen {
private bool loaded = false;
void TestHistoryScreen_Opened(System.Object sender, System.EventArgs e)
{
if (!this.loaded)
{
for (int i = 0 ; i < 100 ; i++)
{
this.TestListBox.AddString(i, "a string " + i);
}
this.loaded = true;
}
}
}
OK, so that code should run the first time the screen is opened. But it doesn't, it runs every time the screen is opened. That tells me I'm getting a brand new TestHistoryScreen object every time I switch to it, because the member variable "loaded" is being set to false again.
If I change the "loaded" variable to static, the list box is only stuffed the first time, but then when I switch to a different screen, and back to TestHistoryScreen, the list box is empty. Again, this indicates that I'm getting a new TestHistoryScreen object every time I switch to the screen.
Re: Screens are constructed every time they are shown?
Posted: Wed Aug 29, 2012 7:35 am
by mark.monroe
The screen with the list box will only be destroyed when the C# garbage collector decides it needs more memory, or you close it. Otherwise that screen is still there. It is just covered up by the screen that gets created when you call show screen. If you were to close all the screens on top of that particular screen, then it should still have it's list box full.
Every time you call Show screen you are creating another screen.
Re: Screens are constructed every time they are shown?
Posted: Wed Aug 29, 2012 7:50 am
by charliehbeijer
Then what click action can I use to go to a previously-created screen, not create a new instance of the screen?
If I highlight a button, and go to Actions, under Click, under Screen, and then highlight Show Screen, it says "Navigates to the specified screen". I don't see anything for navigating to a specified instance of a specified screen.
Re: Screens are constructed every time they are shown?
Posted: Wed Aug 29, 2012 7:58 am
by mark.monroe
Like I mentioned before, you need to close the screens that are on top of the screen with the filled list box. Your right, there is no way to navigate to a specific instance of a screen.