我有一个Windows Phone 8.1应用程序 - 我是Win Phone开发的新手!
它包含各种页面和自定义UserControl。
我正在尝试暂停该应用以访问文件系统/相机。
最初我收到错误是因为我在Frame.Navigate
中传递了复杂的对象,但我通过将复杂对象添加到SuspensionManager.SessionState["..."]
并在OnNavigatedTo
函数中检索它来解决这个问题。
现在,我在应用程序暂停时收到的错误是:
键入' Windows.UI.Xaml.UIElement'无法序列化。请考虑使用DataContractAttribute属性对其进行标记,并使用DataMemberAttribute属性标记要序列化的所有成员。或者,您可以确保该类型是公共的并且具有无参数构造函数 - 然后将序列化该类型的所有公共成员,并且不需要任何属性。
我尝试按照建议操作 - 将[DataContract]
属性添加到类中,并将[DataMember]
添加到所有属性和字段中。
我仍然遇到同样的错误。谷歌并没有提供很多帮助,除了尝试上述内容,所以我通过每个自定义UserControl并将属性添加到那些 - 仍然没有工作,所以我也将属性添加到每个页面 - 主要是出于绝望。
所以现在一切都应该是可序列化的,但我仍然得到同样的错误!我怀疑它有用,但这是堆栈跟踪:
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message,Type type) 在System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle,Type type,SerializationMode mode) 在System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper..ctor(Type type) 在System.Runtime.Serialization.ClassDataContract..ctor(类型类型) 在System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle,Type type,SerializationMode mode) 在System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper..ctor(Type type) 在System.Runtime.Serialization.ClassDataContract..ctor(类型类型) 在System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle,Type type,SerializationMode mode) 在System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper..ctor(Type type) 在System.Runtime.Serialization.ClassDataContract..ctor(类型类型) 在System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle,Type type,SerializationMode mode) 在System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper..ctor(Type type) 在System.Runtime.Serialization.ClassDataContract..ctor(类型类型) 在System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type) 在System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle,Type type,SerializationMode mode) 在System.Runtime.Serialization.DataContract.CheckAndAdd(Type type,Dictionary
2 typesChecked, Dictionary
2& nameToDataContractTable) 在System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContractsForKnownTypes(IList`1 knownTypeList) 在System.Runtime.Serialization.DataContractSerializer.get_KnownDataContracts() 在System.Runtime.Serialization.XmlObjectSerializerContext.get_SerializerKnownDataContracts() 在System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContractFromSerializerKnownTypes(XmlQualifiedName qname) at System.Runtime.Serialization.XmlObjectSerializerContext.ResolveDataContractFromKnownTypes(String typeName,String typeNs,DataContract memberTypeContract) 在System.Runtime.Serialization.XmlObjectSerializerContext.IsKnownType(DataContract dataContract,Type declaredType) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,Boolean verifyKnownType,RuntimeTypeHandle declaredTypeHandle,Type declaredType) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle objectTypeHandle,Type objectType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle,Type declaredType) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle) 在WriteKeyValueOfstringanyTypeToXml(XmlWriterDelegator,Object,XmlObjectSerializerWriteContext,ClassDataContract) 在System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context) 在WriteArrayOfKeyValueOfstringanyTypeToXml(XmlWriterDelegator,Object,XmlObjectSerializerWriteContext,CollectionDataContract) 在System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle) 在System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer,Object graph,DataContractResolver dataContractResolver) 在System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer,Object graph,DataContractResolver dataContractResolver) 在System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer,Object graph,DataContractResolver dataContractResolver) 在System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer,Object graph) 在System.Runtime.Serialization.XmlObjectSerializer.WriteObject(Stream stream,Object graph) 在eAuditWindowsPhone.Common.SuspensionManager.d__0.MoveNext()
我真的不确定下一步该尝试什么?我欢迎一些建议。
答案 0 :(得分:1)
您正在尝试从UI序列化某些元素。这几乎不可能,你不应该这样做。您需要使UserControl中出现的所有内容,每个属性及其类型都可序列化。
您只能将属性(DataContract或DataMember)添加到您自己创建的类中,并且您将无法使UI元素可序列化。 可能是,你的UserControl是一个DataContract,但它仍然有一些UIElement类型的属性,它们不可序列化。
<强>答案:强> 您应该只存储您感兴趣的值(如TextBox.Text或ToggleButton.IsActive,...)。