2016-01-18 100 views
1

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

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

不,你不明确*序列化Geopoint。但是,你觉得'RestorableState'呢? –

回答

1

,而不是试图序列Windows.Devices.Geolocation.Geopoint,只是序列化的字符串数组形式的坐标,使用GeoJSON的或地理散列。

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

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

因此,您需要从不能序列化的类型中移除[RestorableState]属性。

+0

我无处不在的地方不会序列化Geopoint。 我只用 Geolocator locator = new Geolocator(); –

+1

@MakeMakeluv - 好吧,我们可以很明显地看到,因为你已经向我们展示了没有代码。 –

+0

你可以发布代码吗? Geolocator内部使用GeoPoint,您需要重写序列化逻辑。 – daryal

相关问题