Telerik blogs
Have you ever wondered how to save the properties of your UI elements with just one line of code? Do you need to load a previous visual state of an UI element? We're proud to introduce the newest member of the Telerik XAML family - the Persistence Framework. With its powerful save and load mechanism, the Persistence Framework allows you to save the UI related properties of your controls in a stream or an isolated storage so that you can retrieve their values later. This way you can easily store the layout of your application and keep your users' preferences. The Persistence Framework will officially be available with the official Q2 2011 release that is coming in the midst of July, however to satisfy your curiosity we've included it in our freshly released Q2 2011 Beta.

Bellow are demonstrated a couple of basic scenarios after which we're sure you'll appreciate the ease of use and simplicity of the Persistence Framework.  Let's begin by showing how to save the current state of an application in a stream. For the sake of simplicity let's assume there's only one control (ex. RadTreeView) on the page and we want to save the state of that control.

<telerik:RadTreeView x:Name="treeView">
   <telerik:RadTreeViewItem Header="Beverages">
       <telerik:RadTreeViewItem Header="Chai" />
       <telerik:RadTreeViewItem Header="Chang" />
       <telerik:RadTreeViewItem Header="Ipoh Coffee" />
       <telerik:RadTreeViewItem Header="Chartreuse verte" />
       <telerik:RadTreeViewItem Header="Sasquatch Ale" />
   </telerik:RadTreeViewItem>
   <telerik:RadTreeViewItem Header="Condiments">
       <telerik:RadTreeViewItem Header="Aniseed Syrup" />
       <telerik:RadTreeViewItem Header="Genen Shouyu" />
       <telerik:RadTreeViewItem Header="Gula Malacca" />
       <telerik:RadTreeViewItem Header="Louisiana Hot Spiced Okra" />
       <telerik:RadTreeViewItem Header="Louisiana Fiery Hot Pepper Sauce" />
   </telerik:RadTreeViewItem>
</telerik:RadTreeView>

Before you start expanding/collapsing items, you can easily save the current state of the RadTreeView with just one line of code:

private Stream stream;
private PersistenceManager manager = new PersistenceManager();
 
private void Save()
{
    this.stream = this.manager.Save(this.treeView);
}

Simple as that! Now you can play with the TreeView - expand, collapse and select items.
If you want to restore the TreeView to its previous state, all you have to do call the Load() method of the PersistenceManager.

private void Load()
{
    this.stream.Position = 0L;
    this.manager.Load(this.treeView, this.stream);
}

To take things a little bit further, let's see how easy the integration with the IsolatedStorage is.
In order to save the persisted properties in an isolated storage, you can use the IsolatedStorageProvider. It exposes the following members:

SaveToStorage() - the method that saves the UIElement properties in the associated file in the isolated storage
LoadFromStorage() - the method that loads the persisted properties from the UIElement's associated file

Please note that you need to set the PersistenceManager's StorageId attached property for each UIElement that will be persisted. The property is used to create a file in the isolated storage for each persisted control, where the control's properties will be kept.

<telerik:RadTreeView x:Name="treeView" telerik:PersistenceManager.StorageId="treeView">
   <telerik:RadTreeViewItem Header="Beverages">
       <telerik:RadTreeViewItem Header="Chai" />
       <telerik:RadTreeViewItem Header="Chang" />
       <telerik:RadTreeViewItem Header="Ipoh Coffee" />
       <telerik:RadTreeViewItem Header="Chartreuse verte" />
       <telerik:RadTreeViewItem Header="Sasquatch Ale" />
   </telerik:RadTreeViewItem>
   <telerik:RadTreeViewItem Header="Condiments">
       <telerik:RadTreeViewItem Header="Aniseed Syrup" />
       <telerik:RadTreeViewItem Header="Genen Shouyu" />
       <telerik:RadTreeViewItem Header="Gula Malacca" />
       <telerik:RadTreeViewItem Header="Louisiana Hot Spiced Okra" />
       <telerik:RadTreeViewItem Header="Louisiana Fiery Hot Pepper Sauce" />
   </telerik:RadTreeViewItem>
</telerik:RadTreeView>

private IsolatedStorageProvider isoProvider = new IsolatedStorageProvider();
 
private void Save()
{
    this.isoProvider.SaveToStorage();
}
private void Load()
{
    this.isoProvider.LoadFromStorage();
}

Things barely can get any easier than that.

To learn more about the Persistence Framework, please visit its documentation page, online examples or simply get the beta bits and play with them. Any feedback would be greatly appreciated.

PS: Here you can download the sample application demonstrating the scenarios described in this blog post.


About the Author

Alexander Fidanov

Software Developer,
System Integration Team

Related Posts

Comments

Comments are disabled in preview mode.