Problem Saving list of objects in isolated storage

I am trying to save a list of objects that I created in isolated storage and be able to display them in a list, automatically creating a title for them. While the code is working, but as soon as I tombstone the application and run it, all my data will be saved except for the list of objects. I think that my problem may be related to how I initialize the list in the first place, or, perhaps, how I show the names. Any help is appreciated.

this code is in my app.xaml.cs application:

public partial class App : Application { public List<my_type> testList = new List<my_type>(); void loadvalues() { IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; List<my_Type> L; if (settings.TryGetValue<List<DrinkSesh>>("Storage", out L)) { testList = L; } } void savevalues() { IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; settings["Storage"] = testList; settings.Save(); } } 

This code is on my main page to add items to the list:

 (Application.Current as App).testList.Add(new my_type()); 

and this code is for implementing titles on the screen on another page:

  public different_class() { { InitializeComponent(); for (i = 0; i < (Application.Current as App).testList.Count; i++) { CreateATextBlock((Application.Current as App).testList[i].Title_ToString(), i); } } private void CreateATextBlock(String title,int num) { testblockname = new TextBlock(); testblockname.Text = (num + 1) + ". " + title; DrList.Children.Add(testblockname); } } 

Thank you in advance!

+4
source share
2 answers

Here is the code that I use to save and load lists of objects from isolated storage.

 public class IsoStoreHelper { private static IsolatedStorageFile _isoStore; public static IsolatedStorageFile IsoStore { get { return _isoStore ?? (_isoStore = IsolatedStorageFile.GetUserStoreForApplication()); } } public static void SaveList<T>(string folderName, string dataName, ObservableCollection<T> dataList) where T : class { if (!IsoStore.DirectoryExists(folderName)) { IsoStore.CreateDirectory(folderName); } string fileStreamName = string.Format("{0}\\{1}.dat", folderName, dataName); using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileStreamName, FileMode.Create, IsoStore)) { DataContractSerializer dcs = new DataContractSerializer(typeof(ObservableCollection<T>)); dcs.WriteObject(stream, dataList); } } public static ObservableCollection<T> LoadList<T>(string folderName, string dataName) where T : class { ObservableCollection<T> retval = new ObservableCollection<T>(); if (!IsoStore.DirectoryExists(folderName)) { IsoStore.CreateDirectory(folderName); } string fileStreamName = string.Format("{0}\\{1}.dat", folderName, dataName); using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileStreamName, FileMode.OpenOrCreate, IsoStore)) { if (stream.Length > 0) { DataContractSerializer dcs = new DataContractSerializer(typeof(ObservableCollection<T>)); retval = dcs.ReadObject(stream) as ObservableCollection<T>; } } return retval; } } 
+7
source

By simply adding your List to your IsolStorageSettings, you rely on the built-in serializer (DataContractSerializer) to serialize your object to save to disk.

Are you sure your object can be serialized and deserialized correctly?

Doing this is probably the easiest way to do this.

If you do it yourself, not this: - DataContractSerializer is slow - DataContractJsonSerializer is faster - Json.net is faster - Binary serialization is faster.

When you serialize yourself, you should also save to the ravter IsolStorageFile than the settings. This can help in performance, and also adds flexibility, which can help in debugging and testing.

+2
source

Source: https://habr.com/ru/post/1340734/


All Articles