我将对象存储到Properties(如果尚未存储):
if (!Application.Current.Properties.ContainsKey("user"))
{
Application.Current.Properties.Add("user", user);
}
它存储了第一个对象。比我把它设置为null。当我尝试再次检查ContainsKey
时,它会引发异常:
System.AggregateException: 'One or more errors occurred.'
SerializationException: Error in line 1 position 264. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value' contains data of the 'http://schemas.datacontract.org/2004/07/Layouts.Models:User' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'User' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.
我应该删除......什么?
答案 0 :(得分:1)
检查https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/application-class/处的文档:
注意:属性字典只能序列化基本类型 存储。试图存储其他类型(例如List可以 无声地失败。
在主题“持久性”中写道:“属性字典会自动保存到设备中。”因此,您会立即获得异常。
解决方法是手动序列化/反序列化User对象。以下是使用JSON序列化的示例:https://codemilltech.com/persist-whatever-you-want-with-xamarin-forms/
答案 1 :(得分:-1)
使用 Application.Current.Properties.Remove( Key )