简体   繁体   English

带隔离存储设置的 FormatException

[英]FormatException with IsolatedStorageSettings

I have a problem when serializing a Dictionary<string,Person> to IsolatedStorageSettings.Dictionary<string,Person>序列化为 IndependentStorageSettings 时遇到问题。 I'm doing the following:我正在做以下事情:

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();
        }

Here is my Person class:这是我的 Person 类:

[DataContract]
public class Person
{
    [DataMember]
    public bool Unlocked { get; set; }
    [DataMember]
    public GeoCoordinate Location { get; set; }
    [DataMember]
    public List<GeoCoordinate> Bounds { get; set; }

    public Person(bool unlocked, GeoCoordinate location, List<GeoCoordinate> bounds)
    {
        this.Unlocked = unlocked;
        this.Location = location;
        this.Bounds = bounds;
    }
}

The code works the first time, however on the second run I get a System.FormatException at the if condition.该代码第一次工作,但是在第二次运行时,我在 if 条件下得到System.FormatException Any help would be highly appreciated thanks.任何帮助将不胜感激,谢谢。

PS: I tried an IsolatedStorageSettings.ApplicationSettings.Clear() but the call to Clear also gives a FormatException . PS:我尝试了一个 IndependentStorageSettings.ApplicationSettings.Clear() 但对Clear的调用也给出了一个FormatException

I have found something new...the exception occurs twenty-five times, or at least that's how many times it shows up in the Output window.我发现了一些新东西……异常出现了 25 次,或者至少是它在“输出”窗口中出现的次数。 However after that, the data is deserialized perfectly.然而在那之后,数据被完美地反序列化。 Should I be worried about the exceptions if they do not stop the execution of the program?如果它们不停止程序的执行,我是否应该担心异常?

EDIT: Here's the call stack when the exception occurs:编辑:这是发生异常时的调用堆栈:

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  

It looks like GeoCoordinate might not be serializable.看起来 GeoCoordinate 可能无法序列化。 You could try creating your own class with two doubles for the storage and then convert them back on retrieval.您可以尝试使用两个双精度存储创建自己的类,然后在检索时将它们转换回。

First make sure that you code will run exception free without saving to IsolatedStorage.首先确保您的代码将无异常运行而无需保存到IsolatedStorage。 Then you could try just setting the value rather than doing an add.然后你可以尝试只设置值而不是添加。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM