简体   繁体   English

Xamarin方法的Newtonsoft Json和System.ObjectDisposedException

[英]Newtonsoft Json and System.ObjectDisposedException with Xamarin method

I have a list of object that I save as a json file like this: 我有一个保存为json文件的对象列表,如下所示:

List<Sede> listSede;
string json = JsonConvert.SerializeObject(listSede);
System.IO.File.WriteAllText(pathToJson+"\filiali.json",json);

Then I read them with this: 然后我读到它们:

StreamReader r = new StreamReader (pathToJson + fileName);  
string jsonread = r.ReadToEnd();
List<Sede> items = new List<Sede> ();
items = JsonConvert.DeserializeObject<List<Sede>>(jsonread);

Class Sede inherit from MKAnnotation and after i get the list of object in items for each one of them I try to add an annotation to a map like this: Sede类从MKAnnotation继承而来,当我获得其中每个项目的对象列表之后,我尝试向地图添加一个注释,如下所示:

foreach (Sede c in items)
                map.AddAnnotation (c);

but it doesn't add any annotation, if I try to inspect the element c I see that there are some field that are not initialized and have an objectDisposedException, here is the image: 但是它没有添加任何注释,如果我尝试检查元素c我会看到有些未初始化的字段具有objectDisposedException,这是图像: 在此处输入图片说明

MKAnnotation is not serializable, so your class that inherits from it is not being completely inflated when you deserialize it. MKAnnotation不可序列化,因此从其继承的类在反序列化时不会完全膨胀。

You could try writing your own JsonConverter that properly handles Sede, or you could do something like this: 您可以尝试编写自己的JsonConverter来正确处理Sede,也可以执行以下操作:

foreach (Sede c in items) {

  // add a constructor to Sede that properly creates a new object based on a partially inflated one  
  Sede cc = new Sede(c);

  map.AddAnnotation (cc);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM