Many applications require a way to store the user’s state so that it can be restored when the application is restarted. For example, you might want to preserve the user’s font choice or book selection. There are numerous approaches to solving this problem in WPF, but none is easier than using the Telerik PersistenceFramework.
All that is required is three very simple steps:
To see this in action, create a new Telerik application and add the following references:
In the XAML we’ll create three controls plus we’ll add two buttons. The first control will be a tree-view. Notice that in the declaration of the RadTreeView control we add the PersistenceManager.StorageID of “treeView”,
The second control will be a standard ComboBox which we’ll pre-populate with four entries. Again, notice that in the declaration of the ComboBox (not a Telerik control) we add the telerik:PersistenceManager.StorageID of “ComboBox”,
We then add two TextBlocks, one to act as a prompt, and the second to collect the user’s name. The second is given a StorageID of "name."
Finally, we add two buttons: Save and Restore. Their event handlers manage storage to Isolated Storage and restoration, respectively, The Save button event handler creates an instance of the IsolatedStorageProvider class and then calls SaveToStorage on that instance. It then informs the user of success.
The restore button’s event handler also obtains an instance of IsolatedStorageProvider, and calls LoadFromStorage with no arguments – all stored items are restored,
To test that our approach works, you can set each of the controls, click on Save, and then change all the controls and click on Restore. The saved state is restored, overwriting any changes you made subsequent to clicking Save.
[Note that in production code you probably won’t add save and restore buttons, but will instead call the save and restore code on shut down and start up respectively. ]
Next, try this: add a StorageID to your window declaration,
Now, move the window, save and the move again. Hit restore and the window moves back to its saved position(!).
Try maximizing the window and saving it, then un-maximize and hit restore – hey! presto! it returns to maximized. The ability to save the state and position of the window alone is worth the price of admission.
Jesse Liberty has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and has been a Distinguished Software Engineer for AT&T and a VP for Information Services for Citibank and a Software Architect for PBS. You can read more on his personal blog or follow him on twitter
Subscribe to be the first to get our expert-written articles and tutorials for developers!