Windows phone 7 如何使用isolatedstoragefile将多组数据写入文件

Windows phone 7 如何使用isolatedstoragefile将多组数据写入文件,windows-phone-7,windows-phone-7.1,windows-phone-8,isolatedstoragefile,Windows Phone 7,Windows Phone 7.1,Windows Phone 8,Isolatedstoragefile,我在我的windows phone应用程序中有一个列表框,它的数据模板中有4个文本框控件,用于表示问题、答案、日期和电子邮件ID 当用户保存这些数据时,我想将它们保存到本地存储器中,以便稍后我可以读取这些数据,以便在应用程序中显示相同的数据。 因此,如何使用isolatedstoragefile将多组数据写入一个文件,每行中的每组数据都包含上述数据(问题、答案、日期和电子邮件) 在阅读每一条记录时,我如何以不同的方式阅读每一列(问题、答案、日期和电子邮件ID)?我知道我们可以在写入时连接它们,在

我在我的windows phone应用程序中有一个列表框,它的数据模板中有4个文本框控件,用于表示问题、答案、日期和电子邮件ID

当用户保存这些数据时,我想将它们保存到本地存储器中,以便稍后我可以读取这些数据,以便在应用程序中显示相同的数据。 因此,如何使用isolatedstoragefile将多组数据写入一个文件,每行中的每组数据都包含上述数据(问题、答案、日期和电子邮件)


在阅读每一条记录时,我如何以不同的方式阅读每一列(问题、答案、日期和电子邮件ID)?我知道我们可以在写入时连接它们,在读取每一行时拆分它们。但我想知道在IsolatedstorageFile api中是否提供了一种方法来读取我上面想要的方式。

就像处理“普通”文件一样

我不太喜欢二进制或XML序列化,所以我会用分号分隔这4个字段,并将每个项目(问题;答案;日期;emailid)写在新行上。将结果字符串保存到文件中


然后在读取时,将整个文件读入一个字符串,按换行符将其拆分,得到所有项目。对于每一行(项目),使用分号将其拆分,得到4个字段,您可以将它们转换为所需的类型。

我使用Silverlight序列化程序,从中将对象序列化到独立存储。它是一个非常好的序列化程序,速度快,文件小。用于保存和检索的代码是

public static void SaveFile(string filename, object serializableObject, Type type)
    {
        using (IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (userStore.FileExists(filename))
            {
                userStore.DeleteFile(filename);
            }

            using (IsolatedStorageFileStream stream = userStore.CreateFile(filename))
            {
                SilverlightSerializer.Serialize(serializableObject, stream);
            }
        }
    }

    public static object LoadSerializedObjectFromFile(string filename, Type type)
    {
        using (IsolatedStorageFile userStore =
          IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (userStore.FileExists(filename))
            {
                using (IsolatedStorageFileStream stream = userStore.OpenFile(filename, FileMode.Open))
                {
                    return SilverlightSerializer.Deserialize(stream);
                }
            }
        }
        return null;
    }
我通常将单个文件包装在存储库中(模式)。例如,在我的Good Deal应用程序中,我有一个DealRepository。在该存储库中,我有一个静态加载方法,如下所示:

 private static IDeal LoadRecentDeal()
    {
        IDeal savedDeals = IsolatedStorageHelper.LoadSerializedObjectFromFile(RecentDealFileName, typeof(Deal)) as Deal;
        if (savedDeals != null)
        {
            return savedDeals;
        }
        else
        {
            return Deal.CreateNewDeal(RecentDealFileName);
        }
    }
内部保存方法如下所示:

public void Save(IDeal deal)
    {
        deal.LastModifiedDate = DateTime.Now;

        // 
        string fileName;
        if (deal.Name == RecentDealFileName)
        {
            fileName = RecentDealFileName;
        }
        else
        {
            fileName = SavedDirectoryName + Path.DirectorySeparatorChar + deal.ID.ToString();
        }

        IsolatedStorageHelper.SaveFile(fileName, deal, typeof(IDeal));       
    }
在IsolatedStorageHelper和repositories上还有一些其他方法,用于执行诸如在多个文件中保存列表并返回它们之类的操作。。。等等。这一切都取决于您的需要-但我建议您查看Silverlight序列化程序,它使它变得非常简单