0

首先,描述可能有点长,有很多代码,但我不想提供我能提供的所有信息。我正在为我的应用程序使用墓碑,我想保存一个序列化类:

    [DataContract]
    private class Tombstone
    {
        [DataMember]
        public UserDetails UserProfile { get; set; }
    }


    // Create instance of the tombstone class
    private Tombstone _tombstone;
    public ProfileSetup()
    {
        _tombstone = new Tombstone();
    }


    //Add data to userProfile
    void UserInformationAccess_OnGetUserDetailsComplete(GetUserDetailsResponse response)
    {
        _tombstone.UserProfile = response.userDetails;
    }

而且我在从...导航的异常中就在这里:

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        if (_tombstone != null)
            this.SaveState("tombstone", _tombstone);
    }

//The state manager class
public static class StateManager
{
    /// <summary>
    /// Saves a key-value pair into the state object
    /// </summary>
    /// <param name="phoneApplicationPage">The phone application page.</param>
    /// <param name="key">The key.</param>
    /// <param name="value">The value.</param>
    public static void SaveState(this PhoneApplicationPage phoneApplicationPage, string key, object value)
    {
        if (phoneApplicationPage.State.ContainsKey(key))
        {
            phoneApplicationPage.State.Remove(key);
        }

        phoneApplicationPage.State.Add(key, value);
    }
}

现在振作起来,异常即将来临:

“安全异常”

   在 System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id,RuntimeTypeHandle typeHandle,Type 类型)
   在 System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type 类型)
   在 System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type 类型)
   在 System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle, Type type, SerializationMode 模式)
   在 System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContract(RuntimeTypeHandle typeHandle,Type 类型)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter,对象 obj,RuntimeTypeHandle objectTypeHandle,类型 objectType,Int32 声明类型 ID,RuntimeTypeHandle 声明类型句柄,类型声明类型)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter,对象 obj,布尔 isDeclaredType,布尔 writeXsiType,Int32 声明类型 ID,RuntimeTypeHandle 声明类型句柄)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter,对象 obj,布尔 isDeclaredType,布尔 writeXsiType,Int32 声明类型 ID,RuntimeTypeHandle 声明类型句柄)
   在 System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi、Object obj、BindingFlags invokeAttr、Binder binder、Object 参数、CultureInfo 文化、Boolean isBinderDefault、Assembly caller、Boolean verifyAccess、StackCrawlMark 和 stackMark)
   在 System.Reflection.RuntimeMethodInfo.InternalInvoke(对象 obj、BindingFlags invokeAttr、Binder binder、Object[] 参数、CultureInfo 文化、StackCrawlMark 和 stackMark)
   在 System.Reflection.MethodBase.Invoke(对象 obj,对象 [] 参数)
   在 System.Runtime.Serialization.XmlFormatWriter.InternalSerialize(MethodInfo 方法信息,对象 memberValue,类型 memberType,布尔 writeXsiType,XmlObjectSerializerWriteContext 上下文,XmlWriterDelegator xmlWriter)
   在 System.Runtime.Serialization.XmlFormatWriter.WriteValue(类型 memberType,对象 memberValue,布尔 writeXsiType,XmlObjectSerializerWriteContext 上下文,XmlWriterDelegator xmlWriter)
   在 System.Runtime.Serialization.XmlFormatWriter.WriteMember(SerializingObject serObj,Int32 memberIndex,ClassDataContract 派生的MostClassContract)
   在 System.Runtime.Serialization.XmlFormatWriter.WriteClass(CallStackElement`1 callStackElement)
   在 System.Runtime.Serialization.XmlFormatWriter.Serialize(XmlObjectSerializerWriteContext 上下文)
   在 System.Runtime.Serialization.XmlFormatWriter.InitializeCallStack(XmlWriterDelegator xmlWriterDel,对象 obj,XmlObjectSerializerWriteContext writeContext,DataContract 合同)
   在 System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,对象 obj,XmlObjectSerializerWriteContext 上下文)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract 数据合同,XmlWriterDelegator xmlWriter,对象 obj,RuntimeTypeHandle 声明的TypeHandle)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle 声明的TypeHandle)
   在 System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer,对象图)
   在 System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer,对象图)
   在 System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer,对象图)
   在 System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter 编写器,对象图)
   在 System.Runtime.Serialization.XmlObjectSerializer.WriteObject(流流,对象图)
   在 Microsoft.Phone.Shell.StreamPersister.Serialize(IDictionary`2 字典,IEnumerable`1 knownTypes)
   在 Microsoft.Phone.Shell.StreamPersister.Save(ShellPage shellPage,字符串键,IDictionary`2 字典,IEnumerable`1 knownTypes)
   在 Microsoft.Phone.Controls.PhoneApplicationPage.InternalOnNavigatedFrom(NavigationEventArgs e)
   在 System.Windows.Navigation.NavigationService.RaiseNavigated(对象内容,Uri uri,NavigationMode 模式,布尔 isNavigationInitiator,PhoneApplicationPage existingContentPage,PhoneApplicationPage newContentPage)
   在 System.Windows.Navigation.NavigationService.Journal_NavigatedExternally(对象发件人,JournalEventArgs 参数)
   在 System.Windows.Navigation.Journal.OnNavigatedExternally(字符串名称,Uri uri,NavigationMode 模式)
   在 System.Windows.Navigation.Journal.ShellPage_NavigatedAway(对象发送者,NavigateAwayEventArgs args)
   在 Microsoft.Phone.Shell.Interop.ShellPageCallback.FireOnNavigateAway(IntPtr thisPage, Direction direction, IntPtr pageNext)

我已经研究了很多与此相关的内容,但我发现是空的:(感谢任何帮助。

[编辑]

像问题的接缝是 TombStone 类具有私有可访问性,我改变了它......但我得到了以下异常:

不应使用数据合同名称“ArrayOfKeyValueOfstringJTokeneJCYCtcq:http://schemas.microsoft.com/2003/10/Serialization/Arrays”键入“Newtonsoft.Json.Linq.JObject”。将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。

这里也是 UserDetails 类

[KnownType(typeof(Phone[]))]
[KnownType(typeof(UserInterest<InterestCategory?, object>))]
[KnownType(typeof(UserInterest<InterestCategory?, object>[]))] 


    [DataContract]
    public class UserDetails
    {
        /// <summary>
        /// Specifies an identifier of user
        /// </summary>
        /// 
        [DataMember]
        public long userId { get; set; }

        /// <summary>
        /// Specifies a nick of user
        /// </summary>
        /// 
        [DataMember]
        public string nick { get; set; }

        /// <summary>
        /// Specifies a full name of user. The field is absent if it has null value
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

        [DataMember]
        public string fullName { get; set; }

        /// <summary>
        /// Specifies a gender of user. The field is absent if it has null value
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [JsonConverter(typeof(EnumAttributeConverter<Gender>))]
        [DataMember]
        public Gender? gender { get; set; }

        /// <summary>
        /// Specifies a birthday of user as string in dd.MM.yyyy format.
        /// The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public string birthday { get; set; }

        /// <summary>
        /// Specifies an e-mail of user. The field is absent if it has null value
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public string email { get; set; }

        /// <summary>
        /// Specifies a website of user. The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public string website { get; set; }

        /// <summary>
        /// Specifies a general information about user. The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public string about { get; set; }

        /// <summary>
        /// Specifies a place of birth for user. The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public Address<Country?>? homeAddress { get; set; }

        /// <summary>
        /// Specifies a place of residence for user. The field is absent if it has null value. 
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public Address<Country?>? currentAddress { get; set; }

        /// <summary>
        /// Specifies a list of user's phones. The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]       
        public Phone[] phones { get; set; }

        /// <summary>
        /// Specifies an URI of avatar of profile. The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public string avatar { get; set; }

        /// <summary>
        /// Specifies a job. The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public string job { get; set; }

        /// <summary>
        /// Specifies a mood status of user as a color.The field is absent in read commands if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public int? mood { get; set; }

        /// <summary>
        /// Specifies a relationship status of user.The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public RelationshipStatus? relationshipStatus { get; set; }

        /// <summary>
        /// Defines a discovery purpose specified by user.The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public DiscoveryPurpose? discoveryPurpose { get; set; }

        /// <summary>
        /// Specifies a list of user interests. The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public UserInterest<InterestCategory?, object>[] interests { get; set; }

        /// <summary>
        /// Specifies a status of user. The field is absent if it has null value.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public string status { get; set; }

        /// <summary>
        /// Specifies an availability status of user.
        /// </summary>
        [JsonConverter(typeof(EnumAttributeConverter<AvailabilityStatus>))]
        [DataMember]
        public AvailabilityStatus availabilityStatus { get; set; }

        /// <summary>
        /// Specifies a location of user. The field is absent if location is undefined.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public Location location { get; set; }

        /// <summary>
        /// Defines if the given user and the requestor interacted previously.
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMember]
        public bool interacted { get; set; }

        /// <summary>
        /// Defines the status of pending friendship request if it exists.
        /// Equals NONE if there is no pending friendship request.
        /// Equals PENDING_IN if the given user sent friendship
        /// request to the user (requestor) who requests extended
        /// information.
        /// Equals PENDING_OUT if the requestor sent friendship
        /// request to the given request.
        /// </summary>
        [JsonConverter(typeof(EnumAttributeConverter<FriendshipRequestStatus>))]
        [DataMember]
        public FriendshipRequestStatus friendshipRequestStatus { get; set; }

        /// <summary>
        /// Defines if the given user was ignored by the requestor.
        /// </summary>
        /// 
        [DataMember]
        public bool ignored { get; set; }
    }
4

2 回答 2

2

异常表明引用的类型之一在序列化程序的上下文中不可访问/可见...问题是您将类定义为私有:

《私课墓碑》

This means that it is only visible to the class in which it is defined.
You need to make this class public.

于 2012-05-14T12:42:39.000 回答
1

问题在于DataContractSerializer序列化UserDetails对象的能力。

Add页面字典的方法State将使用 DataContractSerializer 序列化对象。

您已经证明您已经Tombstone使用 DataContract 序列化属性标记了该类,但是呢UserDetails

幸运的是,您可以通过尝试自己序列化和反序列化对象来调试此问题。
您可能还会发现使用另一个序列化程序(json.net 很好且易于使用)可以获得更好的性能,因为它的DataContractSerializer速度非常慢。

于 2012-05-09T21:23:11.827 回答