Version

Getting Started with Infragistics Control Persistence Framework

Before You Begin

The main purpose of the Infragistics Control Persistence Framework is to provide a unified API that allows you to save and load a control’s state. You can store a single control’s settings or the settings of multiple controls when using groups.

What You Will Accomplish

This topic will walk you through how to get started with the Infragistics Control Persistence Framework. You will implement functionality for saving and loading the xamDialogWindow control settings. These settings will be stored in isolated storage.

Follow these Steps

  1. Create a Microsoft® WPF™ project

  1. In the Solution Explorer, add the following NuGet package references to your project. For more information on setting up the NuGet feed and adding NuGet packages, you can take a look at the following documentation: NuGet Feeds.

    • Infragistics.WPF.Persistence

    • Infragistics.WPF.DialogWindow

  1. Add the following namespace

    In XAML:

    xmlns:ig="http://schemas.infragistics.com/xaml"
  1. There is a xamDialogWindow control and two buttons on the page. The controls are placed into two rows of a Grid container.

    In XAML:

    <Grid x:Name="LayoutRoot">
       <Grid.RowDefinitions>
          <RowDefinition Height="*" />
          <RowDefinition Height="Auto" />
    
       </Grid.RowDefinitions>
       <!-- Add more elements -->
    </Grid>
  1. Add the xamDialogWindow control

    In XAML:

    <ig:XamDialogWindow x:Name="myDialog" Height="200" Width="300"
        IsMoveable="True" IsResizable="True" RestrictInContainer="True"
        StartupPosition="Center" Grid.Row="0" />
  1. Add the two buttons in the StackPanel container

    In XAML:

    <StackPanel Orientation="Horizontal" Grid.Row="1">
        <Button x:Name="BtnSave" Click="BtnSave_Click" Height="22" Width="100" Content="Save" />
        <Button x:Name="BtnLoad" Click="BtnLoad_Click" Height="22" Width="100" Content="Load" />
    </StackPanel>
  1. Add the following using/Import directives so that you don’t have to type out a member’s fully qualified name.

    In Visual Basic:

    Imports System.IO
    Imports System.IO.IsolatedStorage
    Imports Infragistics
    Imports Infragistics.Controls.Interactions
    Imports Infragistics.Persistence

    In C#:

    using System.IO;
    using System.IO.IsolatedStorage;
    using Infragistics;
    using Infragistics.Controls.Interactions;
    using Infragistics.Persistence;
  1. In the code behind, add functionality to save the xamDialogWindow control’s properties and to handle BtnSave_Click event.

    In Visual Basic:

    Private fileName As String = "tempFile"
    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    ' Save xamDialogWindow settings into a MemoryStream
    Dim memoryStream As MemoryStream = PersistenceManager.Save(myDialog)
    ' Obtains user-scoped isolated storage for use by an application
    Using iso As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
        If iso.FileExists(fileName) Then
            iso.DeleteFile(fileName)
        End If
        Using stream As New IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, iso)
           ' Write file in the isolated storage
           stream.Write(memoryStream.ToArray(),0,CInt(memoryStream.Length))
        End Using
    End Using
    End Sub

    In C#:

    private const string fileName = "tempFile";
    private void BtnSave_Click(object sender, RoutedEventArgs e)
    {
       // Save xamDialogWindow settings into a MemoryStream
       MemoryStream memoryStream = PersistenceManager.Save(myDialog);
       // Obtains user-scoped isolated storage for use by an application
       using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
       {
          if (iso.FileExists(fileName))
             iso.DeleteFile(fileName);
          using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, iso))
          {
             // Write file in the isolated storage
             stream.Write(memoryStream.ToArray(), 0, (int)memoryStream.Length);
          }
       }
    }
  1. Add functionality to load the xamDialogWindow control’s properties and to handle the BtnLoad_Click event.

    In Visual Basic:

    Private Sub BtnLoad_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Using iso As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
        If iso.FileExists(fileName) Then
            Using stream As New IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, iso)
                PersistenceManager.Load(myDialog, stream)
            End Using
        End If
    End Using
    End Sub

    In C#:

    private void BtnLoad_Click(object sender, RoutedEventArgs e)
    {
        using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (iso.FileExists(fileName))
            {
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, iso))
                {
                    PersistenceManager.Load(myDialog, stream);
                }
            }
        }
    }
  1. Save and run your application. You can save the xamDialogWindow properties by pressing the Save button; afterwards, you can resize and/or move the xamDialogWindow and click the Load button to restore the previous state of the control.