代码之家  ›  专栏  ›  技术社区  ›  Paul Martinez

为什么在我重新启动应用程序时IsolatingStorageSettings为空?(第8页)

  •  0
  • Paul Martinez  · 技术社区  · 11 年前

    我使用IsolatingStorageSetting存储对象集合。当我的应用程序运行时,它工作得很好,但当我重新启动它时……集合是空的。。。为什么它不将我的对象集合保存在内存中。?

     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
                }
    

    编辑

    关于保存方法的问题

    信息补充:无法序列化类型“System.Windows.Media.ImageSource”。考虑使用DataContractAttribute属性标记它,并使用DataMemberAttribute属性标记要序列化的所有成员。或者,您可以确保该类型是公共的,并且具有无参数构造函数-然后将序列化该类型的所有公共成员,并且不需要任何属性。

    1 回复  |  直到 11 年前
        1
  •  0
  •   Romasz    11 年前

    可能是因为你没有保存它-尝试使用 IsolatedStorageSettings.ApplicationSettings.Save() 当你完成它时。

    当然,当您重新启动Emulator时,它不会工作-重新启动后,它是一个新的实例。

    编辑 -OP编辑后

    无法序列化 ImageSource -类似的问题是 here 。请考虑序列化ImagePath。