Question

I use the IsolatingStorageSetting to store a collection of objects. It works great when my app is running but when I restart it...the collection is empty...Why does it not keep my object collection in memory.?

 private  void llsElements_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            LongListSelector llselement = null;
            listElementCollection.Clear();

            if (sender != null)
                llselement =(LongListSelector)sender;

            if(llselement.SelectedItem!=null)
            {
               BdeskElement bdelement=(BdeskElement)llselement.SelectedItem;

               if (bdelement.TypeElement == BdeskElement.BdeskTypeElement.File)
               {

                   if (!IsolatedStorageSettings.ApplicationSettings.Contains(bdelement.URLElement))//IsCached? =>NON
                   {

                       if (IsolatedStorageSettings.ApplicationSettings.Count >= 100)//Si la liste des fichiers en cache est pleine
                       {
                           string KeyOldestElement = IsolatedStorageSettings.ApplicationSettings.Last().Key;
                           IsolatedStorageSettings.ApplicationSettings.Remove(KeyOldestElement);//on supprime le dernier élément de la liste
                           if (IsolatedStorageOperations.IsFileSaved(KeyOldestElement+bdelement.Extension))
                           IsolatedStorageOperations.DeleteFile(KeyOldestElement+bdelement.Extension);

                       }

                       IsolatedStorageSettings.ApplicationSettings.Add(bdelement.URLElement, bdelement);//on ajoute notre élément

                        DownloadAndReadFile(bdelement);

                   }

                   else  //Si le fichier est deja présent dans la liste (donc déja en cache)
                   {
                       if (IsFileModified(bdelement, IsolatedStorageSettings.ApplicationSettings[bdelement.URLElement]))//compare la version téléchargée et la version en cache/ les versions sont identiques
                       {
                           string FileNameFormated = bdelement.URLElement.Replace("/", "_").Substring(7, bdelement.URLElement.Length - 7);
                           if (IsolatedStorageOperations.IsFileSaved(FileNameFormated))
                           {
                               MessageBox.Show("Le fichier est lu dans le cache");
                               byte[] Encryptedbytefile = IsolatedStorageOperations.GetFile((FileNameFormated));
                               byte [] UnCryptedByteFile=EncryptedString.DecryptDataToData(Encryptedbytefile);
                               IsolatedStorageOperations.SaveToFile(UnCryptedByteFile, "FileToRead" + bdelement.Extension);
                               IsolatedStorageOperations.ReadFile("FileToRead"+bdelement.Extension);

                           }

                       }

                       else//les versions sont différentes
                       {
                           IsolatedStorageSettings.ApplicationSettings.Remove(bdelement.URLElement);//supprime l'ancienne version
                           IsolatedStorageSettings.ApplicationSettings.Add(bdelement.URLElement, bdelement);//ajoute la nouvelle

                           DownloadAndReadFile(bdelement);
                       }


                   }
               }
               else if (bdelement.TypeElement == BdeskElement.BdeskTypeElement.Folder)
               {

                   //l'élément cliqué devient l'élément courant
                   App.CurrentFolder = bdelement;
                   //On raffiche la page
                   NavigationService.Navigate(new Uri(String.Format("/Views/BDocs/FolderView.xaml?id={0}", Guid.NewGuid().ToString()), UriKind.Relative));

               }
            }
 IsolatedStorageSettings.ApplicationSettings.Save(); //EDITED
            }

EDIT

ISSUE I HAVE ON THE SAVE METHOD

Informations supplémentaires : Type 'System.Windows.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.

Was it helpful?

Solution

Probably it's because you aren't saving it - try to use IsolatedStorageSettings.ApplicationSettings.Save() when you finsh with it.

Of course it won't work when you restart the Emulator - after restarting it is a fresh instance.

EDIT - after OP's edit

You cannot serialize ImageSource - similar question is here. Instead consider serializing ImagePath.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top