问题在隔离存储中存储对象列表

RJD*_*ubz 4 c# windows list stackpanel windows-phone-7

我试图存储我在隔离存储中创建的对象列表,并能够通过自动为它们生成标题在列表中显示它们.到目前为止,代码仍然有效,但是一旦我对应用程序进行了逻辑删除并启动它,除了对象列表之外,我的所有数据都会被保存.我认为我的问题可能在于我首先如何初始化列表,或者可能是我如何显示名称.任何帮助表示赞赏.

这段代码在我的App.xaml.cs中:

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();
        }
     }
Run Code Online (Sandbox Code Playgroud)

此代码在我的mainPage上将项添加到列表中:

(Application.Current as App).testList.Add(new my_type());
Run Code Online (Sandbox Code Playgroud)

这段代码是在不同的页面上将标题实现到屏幕上:

 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);
        }
}
Run Code Online (Sandbox Code Playgroud)

先感谢您!

Ste*_*rne 7

这是我用来保存和加载来自独立存储的对象列表的代码.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)