7

我正在将我在 Windows Phone 中发布的应用程序移植到 Win 8。在尝试写入IsolatedStorage等效应用程序时 ApplicationDataContainer,我遇到了一个异常。例外说

错误:状态管理器设置的大小已超出限制

我不确定这是否是使用ApplicationDataContainer.

public void WriteToIsolatedStorage()
    {
        try
        {

            ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
            ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();

            if (localSettings.Containers.ContainsKey("LoveCycleSetting"))
            {
                localSettings.DeleteContainer("LoveCycleSetting");
            }

            composite["GetWeekStart"] = m_bWeekStart;

            composite["iHistCount"] = m_iHistCount;

            composite["dtHistory"] = this.DateTimeToString(m_dtHistory);

            composite["avgCycleTime"] = m_iAvgCycleTime;
       }
    }

异常发生在倒数第二行。m_dtHistory 是一个大小为 400 的字符串数组。那么它ApplicationDataCompositeValue有固定大小吗?还是我必须将 m_dtHistory 数组写入文件?因为在 WindowsPhone 中我可以直接将数组写入IsolatedStorageSettings.

如果有人可以指导我或提供链接,那将非常有帮助。

阿尔法

4

4 回答 4

6

是的,具有讽刺意味的是,手机上的设置存储比 WinRT 更容易。您可以只序列化到文件。这是我所做的(部分复制自 中的代码SuspensionManager.cs),它适用于值类型和引用类型。

    internal static async Task<bool> SaveSetting(string Key, Object value)
    {
        var ms = new MemoryStream();
        DataContractSerializer serializer = new DataContractSerializer(value.GetType());
        serializer.WriteObject(ms, value);
        await ms.FlushAsync();

        // Uncomment this to preview the contents being written
        /*char[] buffer = new char[ms.Length];
        ms.Seek(0, SeekOrigin.Begin);
        var sr = new StreamReader(ms);
        sr.Read(buffer, 0, (int)ms.Length);*/

        ms.Seek(0, SeekOrigin.Begin);
        StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(Key, CreationCollisionOption.ReplaceExisting);
        using (Stream fileStream = await file.OpenStreamForWriteAsync())
        {
            await ms.CopyToAsync(fileStream);
            await fileStream.FlushAsync();
        }
        return true;
    }

    // Necessary to pass back both the result and status from an async function since you  can't pass by ref
    internal class ReadResults
    {
        public bool Success { get; set; }
        public Object Result { get; set; }
    }
    internal async static Task<ReadResults> ReadSetting<type>(string Key, Type t)
    {
        var rr = new ReadResults();

        try
        {
            var ms = new MemoryStream();
            DataContractSerializer serializer = new DataContractSerializer(t);

            StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(Key);
            using (IInputStream inStream = await file.OpenSequentialReadAsync())
            {
                rr.Result = (type)serializer.ReadObject(inStream.AsStreamForRead());
            }
            rr.Success = true;  
        }
        catch (FileNotFoundException)
        {
            rr.Success = false;
        }
        return rr;
    }
于 2012-09-30T13:09:57.843 回答
1

每个设置的名称最长为 255 个字符。每个设置的大小最大为 8K 字节,每个复合设置的大小最大为 64K 字节。

https://msdn.microsoft.com/library/windows/apps/windows.storage.applicationdata.localsettings.aspx

于 2016-01-11T07:21:39.137 回答
0

我在某处读到但丢失了大小为 64KB 的参考

于 2012-07-10T18:02:56.897 回答
0
    public static void StoreConfig(string content)
    {
        IEnumerable<string> strs = Split(content, 2000);
        int i = 1;
        foreach(var s in strs)
        {
            AppLocalSettings.Values["test" + (i++)] = s;
        }

        AppLocalSettings.Values["test_count"] =  i-1 +"";
    }

    public static string ReadConfig()
    {
        string s = "";
        int count = Convert.ToInt32(AppLocalSettings.Values["test_count"]);
        for(int i = 1; i<=count; i++)
        {
            s += Convert.ToString(AppLocalSettings.Values["test" + (i)]);
        }
        return s;
    }
于 2019-04-05T12:19:30.087 回答