c# - ApplicationDataCompositeValue 的大小

标签 c# windows-8 microsoft-metro

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

Error : The size of the state manager setting has exceeded the limit

我不确定这是否是使用 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

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

阿尔法

最佳答案

是的,具有讽刺意味的是,手机上的设置存储比 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;
    }

关于c# - ApplicationDataCompositeValue 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11114912/

相关文章:

c# - 找不到适合指定文化或中性文化的任何资源

c# - 使用 C# 创建带有自定义按钮的消息框

c# - 通过 LINQ 更改此循环?

c# - 如何将 xaml flipview 限制为仅在一个方向上翻转?

c# - 以编程方式关闭win8应用程序

c# - 具有自动重置功能的计时器导致 System.OutOfMemoryException

java - 我的程序不会在 Windows 8 上创建目录,但会在 Windows 7 上创建目录

html - 在应用栏外使用 Windows 8 默认图标集

.net - Windows 8 从桌面应用程序启动 "metro"应用程序?

c++ - "UWP"和 "Immersive"app 是同一个东西吗?如果不是,有什么区别?