在Windows Store应用中存储数据

6

简洁版: 如何使用C#在Windows Store应用中储存“大”数据?

详细版: 我有一个 JSON 字符串,它可以作为字符串(约 65 KB)或者序列化后的对象进行存储。我最初认为可以使用下面这样的方法:

    public static void SaveData(string param, object value)
    {
        Windows.Storage.ApplicationDataContainer settings = Windows.Storage.ApplicationData.Current.LocalSettings;       
        Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue();
        composite[param] = value;
        settings.Values[param] = composite;
    }

我尝试了这个方法,但我的应用程序显示以下错误:
WinRT信息:尝试在应用程序数据复合值中写入设置时出错
这个错误并没有真正帮助我,它只是不想保存我的数据。我刚刚在网上读到,使用C#编写的Windows Store应用程序中本地设置中只允许存储极少量的字节。
有没有一种快速、安全、优雅的方法来存储我的字符串/对象?我真的需要自己将其写入文件吗?对于仅此字符串而言,使用SQLLite也太过繁琐。

附注:将来请发布您收到的实际错误消息。 即使您不理解它,这里的某个人可能会理解,而且无论如何,它都有助于其他在谷歌上搜索此内容的人。 - NotMe
1
你是对的,我添加了它:“WinRT 信息:尝试在应用程序数据复合值中写入设置时出错” - andreas
4个回答

10

3
考虑到 ApplicationDataContainer 的目的仅仅是为了应用程序设置,那么大小限制是完全合理的。每个设置的限制为8kb;这对于应用程序设置来说绰绰有余。将大量的数据存储在文件或数据库中并不是唯一的方式,也是正确的方式。

我同意第一个观点。那么你建议使用文件还是数据库?能否展示一些工作流程的草图以便了解如何完成它将会很好。 - andreas
@andreas:如果你只是读写一个简单的对象,那就坚持使用文本文件,这样更容易使用。如果你有多个这样的对象和/或需要查询语言带来的强大功能,则应选择数据库。但是,根据你的描述,我认为你不需要费心去使用数据库。 - NotMe

1

为了帮助您,我甚至在这里提供代码:- 请使用以下LocalFolder而不是LocalSettings。

public static class AppSystem
{
    #region FileExistsMethod
    public static async Task<bool> FileExists(string filename)
    {
        StorageFolder folder = ApplicationData.Current.LocalFolder;
        IReadOnlyList<StorageFile> list_of_files = await folder.GetFilesAsync();

        foreach(StorageFile file in list_of_files)
        {
            if (file.DisplayName == "Data")
                return true;
        }
        return false;
    }
    #endregion
    public static async Task<ObservableCollection<Notes>> GetCollection()
    {
        ObservableCollection<Notes> temp = new ObservableCollection<Notes>();

        StorageFolder folder = ApplicationData.Current.LocalFolder;

        if (await AppSystem.FileExists("Data"))
        {
            StorageFile file = await folder.GetFileAsync("Data.txt");
            using (Stream stream = await file.OpenStreamForReadAsync())
            {
                DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Notes));
                temp = (ObservableCollection<Notes>)json.ReadObject(stream);
            }
        }   
        return temp;
    }

    public static async void Save(Notes notes)
    {
        ObservableCollection<Notes> temp = new ObservableCollection<Notes>();
        StorageFolder folder = ApplicationData.Current.LocalFolder;
        bool exists = await AppSystem.FileExists("Data");

        if(exists==true)
        {
            StorageFile file = await folder.GetFileAsync("Data");
            using(Stream stream = await file.OpenStreamForReadAsync())
            {
                DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Notes));
                temp = (ObservableCollection<Notes>)json.ReadObject(stream);
            }
            await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
        }

        temp.Add(notes);

        StorageFile file1 = await folder.CreateFileAsync("Data", CreationCollisionOption.ReplaceExisting);

        using(Stream stream = await file1.OpenStreamForWriteAsync())
        {
            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Notes));
            json.WriteObject(stream, temp);
        }
    }
}

每当我希望使用保存和加载数据时,我喜欢创建一个静态类并将所有方法放在那里,然后在需要时在应用程序中使用它。 这里,Notes是该类,并且我通过将Note对象序列化为JSON字符串来存储Note对象列表。

-2
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

string _value="You value";

localSettings.Values["Your Variable name"]=_value;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接