Reputation: 128
Currently, I am using WPF. I have two windows I-e window1 and window2. window1 has a button to navigate to window2; window2 has also a button to go back to window1. window2 has many controls also user-controls. The case is whenever I navigate window1 to window2, I have to create new object of window2 due to some reasons. I know singleton pattern but cant apply here.
Now, on unload event of window2 I saved window2's object in a static class(I save "this" to static class). when user will again navigate here, I want to load saved object rather than constructing new object.
Is there any method to load saved object in current object that's going to construct ? I-e somewhere in constructor or load I may load saved object?
Thanks in advance
Upvotes: 0
Views: 108
Reputation: 21989
As it is tagged wpf
, you definitely should take a look at mvvm
. Instead of passing data between forms you will have two ViewModel
s (for each form) to hold all associated with UI data and some Model
s to hold the rest. And those view models (and models) will persist between forms closing/opening, means no need to pass anything or save/load.
If you need to actually save data (to example, different sets of states or for the data to survive exit/start application), then have a look at serialization. My favorite is XmlSerializer
/XmlDeserializer
, as it's easy to control, easy to change (edit data) and easy to support versioning. There are dozens of tutorials around, simply search for "c# serialization" with optional words "xml", or "binary" or even "protobuf".
Upvotes: 2