不能序列化自定义类
本文关键字:自定义 序列化 不能 | 更新日期: 2023-09-27 18:06:53
我是Xamarin的新手,我正在尝试为Android/iOS制作应用程序。我试图发送一个带有putExtra的类,所以我试图通过添加isserializable接口来使类序列化,但是每当我尝试这样做时,我都需要实现Handle()和Dispose()方法。我不确定如何处理这些方法,所以它们有一个NotImplementedException。
当我尝试去下一个活动时,我得到NotImplementedException(很可能来自句柄或dispose)。我想知道我是否在类的实现中做错了什么。
这里是事件类(我试图发送到下一个活动):
public class Event : Object, ISerializable
{
private long id;
private string name;
private string description;
private double latitude = 0;
private double longitude = 0;
private User author;
private List<User> participants;
private List<EventDate> eventDates;
public Event(long id, string name, string description, double latitude, double longitude, User author, List<User> participants, List<EventDate> eventDates)
{
this.id = id;
this.name = name;
this.description = description;
this.latitude = latitude;
this.longitude = longitude;
this.author = author;
this.participants = participants;
this.eventDates = eventDates;
}
public Event(long id, string name, string description, User author)
{
this.id = id;
this.name = name;
this.description = description;
this.author = author;
this.participants = new List<User>();
this.eventDates = new List<EventDate>();
}
public long Id
{
get { return id; }
}
public string Name
{
get { return name; }
}
public string Description
{
get { return description; }
}
public double Latitude
{
get { return latitude; }
}
public User Author
{
get { return author; }
}
public List<User> Participants
{
get { return participants; }
}
public List<EventDate> EventDates
{
get { return eventDates; }
}
public IntPtr Handle
{
get
{
throw new NotImplementedException();
}
}
public EventDate getBestDate()
{
EventDate bestDate = null;
foreach (EventDate date in eventDates)
{
if (bestDate == null || date.AvailableUsers.Count > bestDate.AvailableUsers.Count)
{
bestDate = date;
}
}
return bestDate;
}
public void addParticipant(User participant)
{
this.participants.Add(participant);
}
public void Dispose()
{
throw new NotImplementedException();
}
}
这个类在(Portable)项目中,而不是在。droid项目中,因为我在iOS中也需要它。
谁能告诉我我做错了什么,或者建议一个替代方案?
您已将ISerializable
更改为Java.IO.ISerializable
。
我已经创建了这个方式
class MySerializable : Object, Java.IO.ISerializable
{
public string Value {get; private set;}
public MySerializable (IntPtr handle, JniHandleOwnership transfer)
: base (handle, transfer)
{
}
public MySerializable ()
{
}
public MySerializable (string value)
{
Value = value;
}
}
更多详细信息,请访问:
https://developer.xamarin.com/api/type/Java.IO.ISerializable/