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.
Hello Ben,
I am glad that my approach was helpful to you. Please feel free to ask if you have any further questions.
Thank you very much. Good idea. I figured out a similar solution.
I have been researching your issue and have created a small sample for you, named PersistenceOfXamGrid. Since you have many XamGrids in your application you have to save the settings for every Grid separately. In order to save the settings silently you can handle the Exit event of the application. This way every time you close the application, the settings you have made would be saved. To load settings silently you can handle the Loaded event of every UserControl. Doing this when a UserControl loads, the settings from the IsolatedStorage would be loaded. Please find the attached sample and feel free to let me know if you have any further questions.