Saving User Settings using Telerik’s PersistenceManager

There are many times when we need a way to save user settings for our Silverlight Applications. We had one such scenario in our application & thanks to Telerik's PersistenceManager this task becomes super easy. In order for you to enable saving the properties/ selected values of the UI controls all you have to do is

  1. Add a reference to
     
    • Telerik.Windows.Controls
    • Telerik.Windows.PersistenceFramework

    Note: If you are not able to find PersistenceFramework binaries in the list of available Telerik assemblies while adding a reference, just look for it into the Telerik Installation folder.

    C:\Program Files (x86)\Telerik\RadControls for Silverlight Q3 2012\Binaries\Silverlight
     

  2. Add a persistenceStorageID to all the UI controls to which you want to save the properties for

    <UserControl x:Class="PersitentSettingDemo.MainPage"
    xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
    ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
    ="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerik
    ="http://schemas.telerik.com/2008/xaml/presentation"
    mc:Ignorable
    ="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
    <RowDefinition Height="35"/>
    <RowDefinition Height="35"/>
    <RowDefinition Height="35"/>
    <RowDefinition Height="35"/>
    <RowDefinition Height="35"/>
    <RowDefinition Height="35"/>
    <RowDefinition Height="35"/>
    <RowDefinition Height="35"/>
    <RowDefinition Height="35"/>
    </Grid.RowDefinitions>
    <telerik:RadNumericUpDown Grid.Row="0" Width="200" telerik:PersistenceManager.StorageId="myNumericUpDown"/>
    <CheckBox Grid.Row="1" Width="200" Content="Check Me" Margin="0,5,0,0" telerik:PersistenceManager.StorageId="myCheckbox"/>
    <telerik:RadComboBox Grid.Row="2" Width="200" Margin="0,5,0,0" telerik:PersistenceManager.StorageId="myComboBox">
    <telerik:RadComboBoxItem Content="Item1"/>
    <telerik:RadComboBoxItem Content="Item2"/>
    <telerik:RadComboBoxItem Content="Item3"/>
    <telerik:RadComboBoxItem Content="Item4"/>           
    </telerik:RadComboBox>
    <RadioButton Content="Check Me" Width="200" Grid.Row="3" Margin="0,5,0,0" telerik:PersistenceManager.StorageId="myRadioButton"/>
    <telerik:RadButton Content="Save Settings" Click="RadButton_Click" Grid.Row="4" Width="200" HorizontalAlignment="Left"/>
    <telerik:RadButton Content="Restore Settings" Click="RadButton_Click_1" Grid.Row="4" Width="200" HorizontalAlignment="Right"/>
    </Grid>
    </UserControl>
     
  3. Save the user settings in Isolated Storage
    IsolatedStorageProvider isoProvider = new IsolatedStorageProvider();
    isoProvider.SaveToStorage();

    And That's all. Just follow the 3 steps & you can now save the properties of all your UI controls & retrieve them when needed.

    Retrieval of the settings is as easy as a method call

    isoProvider.LoadFromStorage();

    Super Easy, isn't it?

    Here is my code behind if you are feeling lazy & just want to reuse it

    using System.Windows;
    using System.Windows.Controls;
    using Telerik.Windows.Persistence.Storage;
    namespace PersitentSettingDemo
    {
    public partial class MainPage : UserControl
    {
    IsolatedStorageProvider isoProvider = new IsolatedStorageProvider();
    public MainPage()
    {
    InitializeComponent();
    }
    private void RadButton_Click(object sender, RoutedEventArgs e)
    {   
    isoProvider.SaveToStorage();
    }
    private void RadButton_Click_1(object sender, RoutedEventArgs e)
    {
    isoProvider.LoadFromStorage();
    }
    }
    }

    To test click on the Save Settings button, then refresh the page & notice that all controls have restored to their default values.

    Now click on the Restore Settings button & notice again that all user settings are now populated again.

    On a side note if you want to save the user settings for all your controls in a container then just attach the StorageId property to that container control instead.

    For example, I could have achieved the same result by adding

    <Grid x:Name="LayoutRoot" telerik:PersistenceManager.StorageId="mygridContainer">

    & removing the storage ID from all individual controls.

    On another side note & an important one; The StorageId could be any arbitrary unique string that the Framework can use to identify the control uniquely.

    More details on it here:

    http://www.telerik.com/help/silverlight/persistence-framework-getting-started.html

    As far as I know, it has to be unique for every instance of IsolatedStorageProvider but I will leave that test to another blog post.

    I will also need to figure out how this behaves in a multiuser scenario. Will write as soon as I have more information about it.

    For Now use this simple technique & nJoy Coding. P.S. Attached the demo project for your reference. 

Next Recommended Reading Top Navigation Bar Using XAML