Save in a Stream

The PersistenceFramework allows you to save the layout of UIElements in a stream. The PersistenceManager exposes the following methods that persist the data in a stream:

  • Save - this method saves the data in a stream. It should receive the UIElement object which properties will be persisted. It returns the stream with the persisted data.

  • Load - this method loads the persisted data. It should receive the UIElement object which properties should be restored and the stream that holds the object's persisted properties.

For the purpose of this tutorial, let's define the following RadTreeView 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: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> 

In order to persist the RadTreeView's properties in a stream, you need to create an instance of the PersistenceManager:

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 

The persisted visual state of the RadTreeView control can be restored using the PersistencaManager.Load method:

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

Please keep in mind that before loading the persisted data from a stream, you need to make sure that the stream's position matches the beginning of the persisted bytes. In case when the persisted data is saved in a new stream, before loading it, you should set the Stream.Position to 0L.

In this article