Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
4970
how to persistent XamGrid for user experience?
posted

I am trying to use Persistent framework for XamGrid setting for users.What I want to do is:

1. When user change setting, save the setting silently, for example, change the column order.

2. When users access the app next time, they can get the setting from the last time they use the app.

3. In my app, there are many Xamgrids, for example, in different usercontrol, I want to this could be done in my app for any xamgrid.

So I check the sample code and try something as below:

In MyUserControl, there is a XamGrid, called MyXamGrid. I put codes in code behind as:

private const string fileName = "tempFile";
PersistenceSettings settings = new PersistenceSettings();

public MyUserControl()
{
  InitializeComponent();
  LoadFromIsolatedStorage(MyXamGrid, fileName);
  settings = PersistenceManager.GetSettings(MyXamGrid);
  if (settings != null)
  settings.Events.PersistenceSaved += new System.EventHandler(Events_PersistenceSaved);
}

void Events_PersistenceSaved(object sender, PersistenceSavedEventArgs e)
 {
     SaveIntoIsolatedStorage(MyXamGrid, fileName);
 }

private void SaveIntoIsolatedStorage(DependencyObject obj, string fileName)
{
     // Save DependencyObject properties into MemoryStream
     MemoryStream memoryStream = PersistenceManager.Save(obj, settings);

     // Obtains user-scoped isolated storage and creates a file with persistence settings
     using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
     {
      if (iso.FileExists(fileName))
         iso.DeleteFile(fileName);

      using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, iso))
      {
         stream.Write(memoryStream.ToArray(), 0, (int)memoryStream.Length);
      }
     }
}

private void LoadFromIsolatedStorage(DependencyObject obj, string fileName)
{
  using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
  {
  if (iso.FileExists(fileName))
    {
      using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, iso))
      {
        //loads persistence settings from IsolatedStorageFileStream into DependencyObject
        PersistenceManager.Load(obj, stream, settings);
      }
    }
  }
}

but it is not working. How to resolve the problem? Sample code please. Thanks.