类型'Windows.Device.Geolocation.Geopoint'不能被序列化。

问题描述 投票:1回答:2

我正在使用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;
    }
c# prism uwp windows-10-mobile
2个回答
2
投票

而不是试图序列化 "Windows.Device.Geolocation.Geopoint "类型。Windows.Devices.Geolocation.Geopoint只要将坐标以字符串数组的形式序列化,使用geojson或geohash即可。

如果你坚持添加一个无参数的构造函数,你将忍受更多的问题。

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

所以你需要删除 [RestorableState] 从不能序列化的类型中获取属性。


0
投票

为了方便其他有同样问题的人使用。

以下是 stackoverflow解决方案#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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.