类型 'Windows.Devices.Geolocation.Geopoint' 无法序列化

Type 'Windows.Devices.Geolocation.Geopoint' cannot be serialized

我正在使用 Prism 6 开发 UWP 应用程序,当我在调试模式下关闭它时,出现错误 Type 'Windows.Devices.Geolocation.Geopoint' cannot be serialized. 在 OnSuspend 时发生。

在其他 classes 发生此错误之前,但已阅读 classes 必须具有无参数构造函数才能成功序列化。这有帮助,但现在 Geopoint 出现错误。

class Geopoint 的默认构造函数在哪里以及如何使用?

错误:

"Type 'Windows.Devices.Geolocation.Geopoint' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required."

堆栈跟踪: System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)\r\n at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)\r\n at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)\r\n at System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContract(RuntimeTypeHandle typeHandle, Type type)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)\r\n at WriteKeyValueOfstringanyTypeToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )\r\n at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)\r\n at WriteArrayOfKeyValueOfstringanyTypeToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )\r\n at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)\r\n

更新: 我明确不在的地方不要序列化 ​​Geopoint。

我只用

private Geopoint _mapCenter;
private Geopoint _myLocation;

[RestorableState]
public Geopoint MyLocation
{
    get { return _myLocation; }
    set { SetProperty(ref _myLocation, value); }
}

[RestorableState]
public Geopoint MapCenter
{
    get { return _mapCenter; }
    set { SetProperty(ref _mapCenter, value); }
}

Geolocator locator = new Geolocator();

private async void GetMyLocation()  
    {
        try
        {
            var location = await locator.GetGeopositionAsync(TimeSpan.FromMinutes(20), TimeSpan.FromSeconds(30));
            MyLocation = location.Coordinate.Point;
        }
        catch (Exception)
        {
            MyLocation = GlobalConst.DefaultGeoPoint;
            LoadingBlockProgressIndicatorValue += 20;
        }

        MapCenter = MyLocation;
        LoadingBlockProgressIndicatorValue += 20;
    }

与其尝试序列化 Windows.Devices.Geolocation.Geopoint,不如以字符串数组形式序列化坐标,使用 geojson 或 geohash。

如果你坚持添加一个无参数的构造函数,你最终会遇到更多问题。

[RestorableState] 标记要序列化的字段。如果字段不能被序列化,那么你就会有一个异常。解决方案是创建一些仅用于序列化和反序列化目的的其他支持字段。

因此您需要从无法序列化的类型中删除 [RestorableState] 属性。

为了方便其他有相同问题的人使用:

关注Whosebug solution #2您还可以进行以下操作:

在反序列化的任何地方添加对您的额外内容的引用 class:

MyType myValue = JsonConvert.DeserializeObject<MyType>(serializedQuery, new JSonGeopositionConverter());

使用专用的反序列化器(在我的示例中不完整,因为它不关心高度等):

class JSonGeopositionConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(Windows.Devices.Geolocation.Geopoint));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jo = JObject.Load(reader);

        BasicGeoposition bgp = new BasicGeoposition
        {
            Longitude = (double)jo["Position"]["Longitude"],
            Latitude = (double)jo["Position"]["Latitude"]
        };
        Geopoint gl = new Geopoint(bgp);
        return gl;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}