本文介绍了Windows Phone中的IsolatedStorageSettings.Save方法:是否保存整个词典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows Phone应用程序中的IsolatedStorageSettings.Save方法是否保存整个字典,而不管我们对其进行的更改如何? IE.如果我们说其中有50个项目,并且只更改了一项,那么Save方法是否会一次又一次地保存(序列化等)整个字典?是否有关于此类的详细文档,并且有人知道秘密使用"哪种数据存储格式吗?

Does the IsolatedStorageSettings.Save method in a Windows Phone application save the whole dictionary regardless of the changes we made in it? I.e. if we have say 50 items in it, and change just one, does the Save method saves (serializes, etc) the whole dictionary again and again? Is there any detailed documentation on this class and does anybody know what data storage format is used "under the hood"?

推荐答案

我设法在Windows Phone SDK随附的Windows Phone模拟器VHD图像的入口中找到了IsolatedStorageSettings.Save方法的实现(答案到此问题这样可以帮助我做到这一点).这是该方法的源代码:

I've managed to find the implementation of the IsolatedStorageSettings.Save method in the entrails of the Windows Phone emulator VHD images supplied with the Windows Phone SDK (the answer to this question on SO helped me to do that). Here is the source code of the method:

public void Save()
{
    lock (this.m_lock)
    {
        using (IsolatedStorageFileStream isolatedStorageFileStream = this._appStore.OpenFile(this.LocalSettingsPath, 4))
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                Dictionary<Type, bool> dictionary = new Dictionary<Type, bool>();
                StringBuilder stringBuilder = new StringBuilder();
                using (Dictionary<string, object>.ValueCollection.Enumerator enumerator = this._settings.get_Values().GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        object current = enumerator.get_Current();
                        if (current != null)
                        {
                            Type type = current.GetType();
                            if (!type.get_IsPrimitive() && type != typeof(string))
                            {
                                dictionary.set_Item(type, true);
                                if (stringBuilder.get_Length() > 0)
                                {
                                    stringBuilder.Append('\0');
                                }
                                stringBuilder.Append(type.get_AssemblyQualifiedName());
                            }
                        }
                    }
                }
                stringBuilder.Append(Environment.get_NewLine());
                byte[] bytes = Encoding.get_UTF8().GetBytes(stringBuilder.ToString());
                memoryStream.Write(bytes, 0, bytes.Length);
                DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(Dictionary<string, object>), dictionary.get_Keys());
                dataContractSerializer.WriteObject(memoryStream, this._settings);
                if (memoryStream.get_Length() > this._appStore.get_AvailableFreeSpace() + isolatedStorageFileStream.get_Length())
                {
                    throw new IsolatedStorageException(Resx.GetString("IsolatedStorageSettings_NotEnoughSpace"));
                }
                isolatedStorageFileStream.SetLength(0L);
                byte[] array = memoryStream.ToArray();
                isolatedStorageFileStream.Write(array, 0, array.Length);
            }
        }
    }
}

因此,正如我们看到的那样,每次调用Save时,整个字典都会被序列化.而且我们可以从代码中看到用于序列化集合值的方法.

So, as we can see the whole dictionary is serialized every time when we call Save. And we can see from code what method is used to serialize the collection values.

这篇关于Windows Phone中的IsolatedStorageSettings.Save方法:是否保存整个词典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:49