繁体   English   中英

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

[英]problem Storing a list of Objects in Isolated Storage

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

这段代码在我的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();
        }
     }

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

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

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

 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);
        }
}

先感谢您!

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

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;
    }
}

通过简单地将您的集合(List)添加到IsolatedStorageSettings,您依靠内置的序列化程序(DataContractSerializer)来序列化您的对象以便持久化到磁盘。

您确定您的对象可以正确序列化和反序列化吗?

自己这样做可能是最简单的方法。

如果你自己这样做,而不是: - DataContractSerializer很慢 - DataContractJsonSerializer更快 - Json.net更快 - 二进制序列化最快。

在自己序列化时,您还应该保存在IsolatedStorageFile rahter中而不是设置中。 这有助于提高性能并增加灵活性,有助于调试和测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM