Getting Started with Silverlight PersistenceFramework

The PersistenceFramework allows you to save the visual state of your application either in an isolated storage or in a Stream.

In order to use the PersistenceFramework in your application, you need to add a reference to the following assemblies:

  • Telerik.Windows.Controls
  • Telerik.Windows.PersistenceFramework

You can find more info here.

Persisted Types

By default the PersistenceManager persists properties of the following types:

  • Primitive Types

  • UIElement

  • IEnumerable

If you want to persist more complex controls with complex properties such as RadGridView and its filtering, sorting and grouping descriptors, you should use a Custom Property Provider. Good examples of its implementation can be found in our SDK Samples Browser

In order to persist the state of a UI component, the PersistenceFramework uses serialization. This means that the framework creates a serialization string describing each persisted component and you are provided with the option to save that string in a stream or in an isolated storage.

Defining PersistenceManager that stores the data in an isolated storage

In order to save the application's layout properties in an isolated storage, you need to define a StorageId for the controls whose layout you need to persist. You can set the StorageId through the telerik:PersistenceManager.StorageId attached property. Then the PersistenceManager will use the value of this property to create a new file in the isolated storage, name it accordingly to the StorageId and store the control's properties in it:

<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:RadTreeViewItem Header="Confections"> 
        <telerik:RadTreeViewItem Header="Teatime Chocolate Biscuits" /> 
        <telerik:RadTreeViewItem Header="Sir Rodney's Marmalade" /> 
        <telerik:RadTreeViewItem Header="Zaanse koeken" /> 
        <telerik:RadTreeViewItem Header="Chocolade" /> 
        <telerik:RadTreeViewItem Header="Maxilaku" /> 
        <telerik:RadTreeViewItem Header="Valkoinen suklaa" /> 
    </telerik:RadTreeViewItem> 
</telerik:RadTreeView> 

The above definition will prepare the RadTreeView control and all its UI related properties to be persisted. The PersistenceManager uses the IsolatedStorageProvider to store and load data to and from an isolated storage. This is why in order to save the RadTreeView's UI properties, you can use the IsolatedStorageProvider.SaveToStorage() method:

IsolatedStorageProvider isoProvider = new IsolatedStorageProvider(); 
isoProvider.SaveToStorage(); 
Dim isoProvider As New IsolatedStorageProvider() 
isoProvider.SaveToStorage() 

And in order to restore the saved RadTreeView layout, you can call the IsolatedStorageProvider.LoadFromStorage() method:

Defining PersistenceManager that stores the layout data in a stream

If you want to save the layout of a UI control in a stream, you can take advantage of the PersistenceManager Save() and Load() methods. The PersistenceManager.Save(object obj) method saves the object's properties in a stream. The PersistenceManager.Load(object obj, Stream stream) method restores the properties from the stream, and if possible applies them to the object. So if you have a RadTreeView definition:

<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:RadTreeViewItem Header="Confections"> 
        <telerik:RadTreeViewItem Header="Teatime Chocolate Biscuits" /> 
        <telerik:RadTreeViewItem Header="Sir Rodney's Marmalade" /> 
        <telerik:RadTreeViewItem Header="Zaanse koeken" /> 
        <telerik:RadTreeViewItem Header="Chocolade" /> 
        <telerik:RadTreeViewItem Header="Maxilaku" /> 
        <telerik:RadTreeViewItem Header="Valkoinen suklaa" /> 
    </telerik:RadTreeViewItem> 
</telerik:RadTreeView> 

you can save the treeView properties in a stream like so:

private Stream stream; 
private void Save(object sender, RoutedEventArgs e) 
{ 
 PersistenceManager manager = new PersistenceManager(); 
 stream = manager.Save(treeView);  
} 
Private stream As Stream 
 
Private Sub Save(sender As Object, e As RoutedEventArgs) 
    Dim manager As New PersistenceManager() 
    stream = manager.Save(treeView) 
End Sub 

Later the layout of the RadTreeView can be restored through the Load() method:

private void Load(object sender, RoutedEventArgs e) 
{ 
 stream.Position = 0L; 
 PersistenceManager manager = new PersistenceManager(); 
 manager.Load(treeView, stream); 
} 
Private Sub Load(sender As Object, e As RoutedEventArgs) 
    stream.Position = 0L 
    Dim manager As New PersistenceManager() 
    manager.Load(treeView, stream) 
End Sub 
In this article