C# 带有IsolatedStorage设置的FormatException

C# 带有IsolatedStorage设置的FormatException,c#,windows-phone-7,xna,windows-phone,isolatedstorage,C#,Windows Phone 7,Xna,Windows Phone,Isolatedstorage,在序列化字典以隔离存储设置时出现问题。我正在做以下工作: public Dictionary<string, Person> Names = new Dictionary<string, Person>(); if (!IsolatedStorageSettings.ApplicationSettings.Contains("Names")) { //Add to dictionary Names.Add(

在序列化
字典
以隔离存储设置时出现问题。我正在做以下工作:

public Dictionary<string, Person> Names = new Dictionary<string, Person>();

if (!IsolatedStorageSettings.ApplicationSettings.Contains("Names"))
        {
            //Add to dictionary
            Names.Add("key", new Person(false,
                new System.Device.Location.GeoCoordinate(0, 0),
                new List<GeoCoordinate>() 
                {
                    new GeoCoordinate(35.8974, 14.5099),
                    new GeoCoordinate(35.8974, 14.5099),
                    new GeoCoordinate(35.8973, 14.5100),
                    new GeoCoordinate(35.8973, 14.5099)
                }));
            //Serialize dictionary to IsolatedStorage
            IsolatedStorageSettings.ApplicationSettings.Add("Names", Names);
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

首先确保您的代码在不保存到IsolatedStorage的情况下无异常运行。然后,您可以尝试只设置值,而不是执行加法

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["Names"] = Names;
settings.Save();

看起来GeoCoordinate可能无法序列化。您可以尝试为存储创建自己的类,其中包含两个double,然后在检索时将它们转换回来。

可能是这个异常,在这种情况下,您可以安全地忽略它:为了确保,您可以提供完整的调用堆栈吗?因此,我不得不向Person类添加一个XNA矩形,FormatException的数量激增。如此之多以至于我的游戏挂起大约五秒钟直到异常停止。有什么想法吗?我不能让它保持这样。已经尝试过了,代码抛出了一个KeyNotFoundException,因此您最终仍然需要在catch中调用Add()。您是对的,就是这样。我创建了一个包含两个double的SerializableGeoCoordinate类,并添加了一个助手方法,该方法从标准GeoCoordinate创建SerializableGeoCoordinate。谢谢你的帮助!
mscorlib.dll!double.Parse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider) + 0x17 bytes  
System.Xml.dll!System.Xml.XmlConvert.ToDouble(string s) + 0x4b bytes    
System.Xml.dll!System.Xml.XmlReader.ReadContentAsDouble() + 0x1f bytes  
System.Runtime.Serialization.dll!System.Xml.XmlDictionaryReader.XmlWrappedReader.ReadContentAsDouble() + 0xb bytes  
System.Runtime.Serialization.dll!System.Xml.XmlDictionaryReader.ReadElementContentAsDouble() + 0x35 bytes   
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlReaderDelegator.ReadElementContentAsDouble() + 0x19 bytes  
mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo rtmi, object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object parameters, System.Globalization.CultureInfo culture, bool isBinderDefault, System.Reflection.Assembly caller, bool verifyAccess, ref System.Threading.StackCrawlMark stackMark)   
mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture, ref System.Threading.StackCrawlMark stackMark) + 0x168 bytes 
mscorlib.dll!System.Reflection.MethodBase.Invoke(object obj, object[] parameters) + 0xa bytes   
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlFormatReader.ReadValue(System.Type type, string name, string ns, System.Runtime.Serialization.XmlObjectSerializerReadContext context, System.Runtime.Serialization.XmlReaderDelegator xmlReader) + 0x138 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlFormatReader.ReadMemberAtMemberIndex(System.Runtime.Serialization.ClassDataContract classContract, ref object objectLocal, System.Runtime.Serialization.DeserializedObject desObj) + 0xc4 bytes    
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlFormatReader.ReadClass(System.Runtime.Serialization.DeserializedObject desObj, System.Runtime.Serialization.ClassDataContract classContract, int membersRead) + 0xf3 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlFormatReader.Deserialize(System.Runtime.Serialization.XmlObjectSerializerReadContext context) + 0x36 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlFormatReader.InitializeCallStack(System.Runtime.Serialization.DataContract clContract, System.Runtime.Serialization.XmlReaderDelegator xmlReaderDelegator, System.Runtime.Serialization.XmlObjectSerializerReadContext xmlObjContext, System.Xml.XmlDictionaryString[] memberNamesColl, System.Xml.XmlDictionaryString[] memberNamespacesColl) + 0x77 bytes    
System.Runtime.Serialization.dll!System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Runtime.Serialization.XmlObjectSerializerReadContext context) + 0x5d bytes  
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlReaderDelegator reader) + 0x3 bytes  
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(System.Runtime.Serialization.XmlReaderDelegator reader, string name, string ns, ref System.Runtime.Serialization.DataContract dataContract) + 0x10e bytes  
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Type declaredType, System.Runtime.Serialization.DataContract dataContract, string name, string ns) + 0xb bytes   
System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContractSerializer.InternalReadObject(System.Runtime.Serialization.XmlReaderDelegator xmlReader, bool verifyObjectName) + 0x124 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(System.Runtime.Serialization.XmlReaderDelegator reader, bool verifyObjectName) + 0xe bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.ReadObject(System.Xml.XmlDictionaryReader reader) + 0x7 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.ReadObject(System.IO.Stream stream) + 0x17 bytes  
System.Windows.dll!System.IO.IsolatedStorage.IsolatedStorageSettings.Reload() + 0xa3 bytes  
System.Windows.dll!System.IO.IsolatedStorage.IsolatedStorageSettings.IsolatedStorageSettings(bool useSiteSettings) + 0x20 bytes 
System.Windows.dll!System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.get() + 0xd bytes  
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["Names"] = Names;
settings.Save();