键入' Windows.Devices.Geolocation.Geopoint'无法序列化

时间:2016-01-18 08:59:43

标签: c# prism uwp windows-10-mobile

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

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

dopavit的默认构造函数在哪里以及如何使用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;
    }

2 个答案:

答案 0 :(得分:2)

不是尝试序列化Windows.Devices.Geolocation.Geopoint,而是序列化字符串数组形式的坐标,使用geojson或geohash。

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

[RestorableState]标记要序列化的字段。如果字段无法序列化,那么您将遇到异常。解决方案是创建一些其他支持字段,这些字段仅用于序列化和反序列化。

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

答案 1 :(得分:0)

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

stackoverflow solution #2之后,您还可以执行以下操作:

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

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();
    }
}
相关问题