IsolatedStorage上的FormatException

时间:2012-05-28 18:37:53

标签: c# silverlight windows-phone-7

我在应用程序设置中存储了一些项目。当我这样做时

this._userSettings = IsolatedStorageSettings.ApplicationSettings;

我得到System.FormatException。当我读到时,如何弄清楚是什么原因造成的呢?我假设在IsolatedStorage中有些东西不应该存在。

这是堆栈。

 at System.Double.Parse(String s, NumberStyles style, IFormatProvider provider)
   at System.Xml.XmlConvert.ToDouble(String s)
   at System.Xml.XmlReader.ReadContentAsDouble()
   at System.Xml.XmlDictionaryReader.XmlWrappedReader.ReadContentAsDouble()
   at System.Xml.XmlDictionaryReader.ReadElementContentAsDouble()
   at System.Runtime.Serialization.XmlReaderDelegator.ReadElementContentAsDouble()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.Runtime.Serialization.XmlFormatReader.ReadValue(Type type, String name, String ns, XmlObjectSerializerReadContext context, XmlReaderDelegator xmlReader)
   at System.Runtime.Serialization.XmlFormatReader.ReadMemberAtMemberIndex(ClassDataContract classContract, Object& objectLocal, DeserializedObject desObj)
   at System.Runtime.Serialization.XmlFormatReader.ReadClass(DeserializedObject desObj, ClassDataContract classContract, Int32 membersRead)
   at System.Runtime.Serialization.XmlFormatReader.Deserialize(XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlFormatReader.InitializeCallStack(DataContract clContract, XmlReaderDelegator xmlReaderDelegator, XmlObjectSerializerReadContext xmlObjContext, XmlDictionaryString[] memberNamesColl, XmlDictionaryString[] memberNamespacesColl)
   at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, DataContract& dataContract)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)
   at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(Stream stream)
   at System.IO.IsolatedStorage.IsolatedStorageSettings.Reload()
   at System.IO.IsolatedStorage.IsolatedStorageSettings..ctor(Boolean useSiteSettings)
   at System.IO.IsolatedStorage.IsolatedStorageSettings.get_ApplicationSettings()
   at ShowSeek.Custom_Classes.StateManager.SetupState()
   at ShowSeek.App.Application_Activated(Object sender, ActivatedEventArgs e)
   at Microsoft.Phone.Shell.PhoneApplicationService.FireActivated(Boolean isExecutionContextPreserved)
   at Microsoft.Phone.Execution.NativeEmInterop.FireOnResume(Boolean isExecutionContextPreserved)

3 个答案:

答案 0 :(得分:0)

Windows Phone Silverlight中的函数double.Parse的实现如下:

public static double Parse(string s, NumberStyles style, IFormatProvider provider)
{
    NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
    NumberFormatInfo instance = NumberFormatInfo.GetInstance(provider);
    double result = 0.0;
    try
    {
        Number.ParseDouble(s, style, instance, true, ref result);
        return result;
    }
    catch (FormatException)
    {
        string str = s.Trim();
        if (str.Equals(instance.PositiveInfinitySymbol))
        {
            return PositiveInfinity;
        }
        if (str.Equals(instance.NegativeInfinitySymbol))
        {
            return NegativeInfinity;
        }
        if (!str.Equals(instance.NaNSymbol))
        {
            throw;
        }
        return NaN;
    }
}

基本上,它首先尝试将你的双读作为数字。然后,如果它失败,它将尝试特殊情况(无穷大,NaN)。这就是方法的实现方式,你无能为力。性能影响应该可以忽略不计,特别是因为它在应用程序生命周期中只出现一次(第一次尝试accessIsolatedStorageSettings.ApplicationSettings时)。

答案 1 :(得分:0)

您以前的设置可能已更改(例如,类定义已更改)。

使用:

IsolatedStorageSettings.ApplicationSettings.Clear(); 

清空以前存储的设置。使用后不要忘记对代码发表评论。

答案 2 :(得分:0)

当我使用传递的InvarientCulture作为IFormatProvider时,我遇到了同样的错误

Convert.ToDouble(Text, CultureInfo.InvariantCulture);