添加到隔离存储WP中保存的列表

时间:2014-12-12 13:46:50

标签: c# windows-phone-8 windows-phone-8.1

我试图调用列表并使用

将新项目添加到列表中
 IsolatedStorageFile Settings1 = IsolatedStorageFile.GetUserStoreForApplication();

if (Settings1.FileExists("MyStoreItems"))
        {
            using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
            {                                
                DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
                listobj = (PanoramaApp5.StorageControl.MyDataList)serializer.ReadObject(fileStream);
                listobj.Add(new MyData { Name = filename, Location = ("/Downloaded/" + filename) });   
                serializer.WriteObject(fileStream, listobj);
            }
        }            

我之前使用过这个

listobj.Add(new MyData { Name = filename, Location = ("/Downloaded/" + filename) });


            if (Settings1.FileExists("MyStoreItems"))
            {
                using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Create))
                {
                    DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
                    serializer.WriteObject(fileStream, listobj);

                }
                MessageBox.Show("Items stored successfully.");
            }

它一直在添加到列表中,直到我要么离开页面还是关闭了应用程序,如果再次调用它就会重写列表。

如何在不破坏旧列表的情况下添加到列表中

编辑*

我使用以下代码写入列表

 public static void writeisobtn_Click(string filename)
    {
        listobj.Add(new MyData { Name = filename });
        using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.OpenOrCreate))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
            serializer.WriteObject(fileStream, listobj);
        }
        MessageBox.Show("Items stored successfully.");

    }

这很好用,如果我使用wptools并获取文件并使用notepad ++打开它我会得到

      <StorageControl.MyData><Name>TestName1</Name></StorageControl.MyData>     
      <StorageControl.MyData><Name>TestName2</Name></StorageControl.MyData>

我使用

调用列表
    using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
                listobj = (MyDataList)serializer.ReadObject(fileStream);
            }    

列表加载正常,我返回页面并添加更多名称到列表中,列表加载新名称Aswel作为旧,但如果我关闭并打开应用程序然后添加ei TestName5当我获取文件并打开它只有一个TestName5条目

2 个答案:

答案 0 :(得分:0)

FileMode.Open,因为您的代码已经明确断言该文件存在。

using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))

您也可以使用(OpenOrCreate)

using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.OpenOrCreate))

然后您不需要FileExists检查。

答案 1 :(得分:0)

我将它保存为像这样保存文件

  public static void writeisobtn_Click(string filename)
    {
        MyDataList listobj = new MyDataList();
        using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
            listobj = (MyDataList)serializer.ReadObject(fileStream);
        }     


        listobj.Add(new MyData { Name = filename });

        using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.OpenOrCreate))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
            serializer.WriteObject(fileStream, listobj);

        }
        MessageBox.Show("Items stored successfully.");

    }