다음 클래스를 사용하여 고객 위치 데이터를 저장하고 있습니다.
public class CustomerData
{
public string CustomerName { get; set; }
public int CustomerNumber { get; set; }
public string CustomerAddress { get; set; }
public string CustomerCity { get; set; }
public string CustomerZip { get; set; }
public string CustomerState { get; set; }
public Geopoint CustomerGeopoint { get; set; }
}
JSON 파일 내부에 ... 그리고 다음과 같은 서비스를 사용하여 데이터를 검색합니다.
public static async Task<ObservableCollection<CustomerData>> GetCustomerData()
{
var folder = ApplicationData.Current.LocalFolder;
var dataFile = await folder.TryGetItemAsync("CustomerData.json") as IStorageFile;
var stringResult = await FileIO.ReadTextAsync(dataFile);
ObservableCollection<CustomerData> CustomersRetrievedData = JsonConvert.DeserializeObject<ObservableCollection<CustomerData>>(stringResult, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
Customers = CustomersRetrievedData;
return await Task.FromResult(CustomersRetrievedData);
}
다음과 같이 데이터를 저장합니다.
public static async void SaveCustomerData()
{
var folder = ApplicationData.Current.LocalFolder;
StorageFile newFile = await folder.CreateFileAsync("CustomerData.json", CreationCollisionOption.ReplaceExisting);
var stringData = JsonConvert.SerializeObject(Customers);
await FileIO.WriteTextAsync(newFile, stringData);
}
내 문제는 모든 geopoint 데이터가 거기에있는 후 GetCustomerData () 메서드에서 데이터를 역 직렬화하여 데이터를 읽으려고하면 다음 오류가 발생한다는 것입니다.
유형에 사용할 생성자를 찾을 수 없습니다
Windows.Devices.Geolocation.Geopoint
. 클래스에는 기본 생성자, 인수가있는 생성자 또는 JsonConstructor 속성으로 표시된 생성자가 있어야합니다.
나는 이것을 고치는 방법을 이해하지 못하고 newtonsoft 문서에서 아무것도 찾을 수 없습니다. 누구든지 이것이 어떻게 수행되는지 알고 있습니까?
에 대한 문서를 살펴보면 Geopoint
3 개의 .ctor가 있다는 것을 알 수 있습니다. JSON.NET에는 오류 메시지에서 볼 수 있듯이 역 직렬화를 위해 매개 변수가없는 .ctor가 필요합니다.
따라서 할 수있는 한 가지는 Geopoint
구조 를 미러링하는 (모든 속성 포함 ) 유형 (사용자가 만든)의 속성을 포함하도록 클래스를 변경 하지만 매개 변수없는 .ctor도 포함하는 것입니다. 필요한 IGeoshape
곳 어디에서나 사용할 수 있도록 구현할 수도 있습니다 IGeoshape
.
라인을 따라 뭔가 :
public class GeopointProxy : IGeoshape {
public GeopointProxy() { }
private AltitudeReferenceSystem _altitudeReferenceSystem;
// This is part of the IGeoshape interface, you can't change it's signature, and it's readonly. Fun.
[JsonIgnore]
public AltitudeReferenceSystem AltitudeReferenceSystem { get { return _altitudeReferenceSystem; } }
public AltitudeReferenceSystem SettableAltitudeReferenceSystem {
get {
return AltitudeReferenceSystem;
}
set {
_altitudeReferenceSystem = value;
}
}
// rinse and repeat as appropriate for the other interface members and class properties
// Then include a conversion function, or get fancy and add type conversion operators:
public Geopoint ToGeopoint() {
return new Geopoint(Position, AltitudeReferenceSystem, SpatialReferenceId);
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다