无法序列化自定义类

时间:2016-09-13 13:19:57

标签: c# android xamarin serializable

我对Xamarin很新,我正在尝试为Android / iOS制作应用程序。我正在尝试使用putExtra发送一个类,所以我尝试通过添加ISerializable接口来创建Serializable类,但每当我尝试这样做时,我需要实现一个Handle()和Dispose()方法。我不知道如何处理这些方法,因此它们有一个NotImplementedException。

当我尝试进行下一个活动时,我得到NotImplementedException(很可能来自句柄或dispose)。我想知道我的班级实施是否有问题。

这是Event类(我尝试将其发送到下一个Activity):

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中也需要它。

有人可以告诉我我做错了什么或者建议替代方案吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您已将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/

希望这会对你有所帮助。

相关问题