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

本文关键字:序列化 Geopoint Geolocation Windows Devices 类型 | 更新日期: 2023-09-27 18:30:57

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

在其他类发生此错误之前,但已阅读类必须具有无参数构造函数才能成功序列化。这有所帮助,但现在是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;
    }

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

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

如果您坚持添加无参数构造函数,则最终会遇到更多问题。

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

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

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

在堆栈溢出解决方案#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();
    }
}