Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
The C# WPF application I am writing is heavily dependant on pages hosted inside a frame control. The pages are accessed by clicking their respective menu items with frame.Navigate(new pageName()); But this means that each time the user wants to go back to a page (which is quite a lot of times) I have to reload all of the data that was in the interface before the page was switched. Currently I am using a separate XML file containing all the data for each page before it is changed which is then checked by the page' loaded event and reloaded into the controls. Is there a better and easier way to access pages over and over again without having to create a new instance of it?

I could initialize all of the pages with public pageName pagename = new PageName(); but it would probably use up unnecessary memory and it would be wasted if the user never accessed one of the pages.

I could carry on initializing a new instance of the page in its menu click event but check if it is already initialized and use that instead before creating a new one.

I could continue using my method of saving to a file and loading from it on the next load of the page.

I think the second one is best but I have no idea how to check if the page I want has already been initialized and can be used. I use this for getting the current instance of a window: var MainWindow = Application.Current.Windows.Cast().FirstOrDefault(window => window is MainWindow) as MainWindow; but it doesn't work for pages. Basically I just want to know how I can use the current instance of a page if it has already been initialized instead of creating a new one each time it needs to be accessed.
Posted

1 solution

try to use navigation history and Page.KeepAlive. Here is MSDN link:
http://msdn.microsoft.com/en-us/library/ms750478(v=vs.110).aspx#NavigationHistory.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900