【问题标题】:Can't serialize custom class无法序列化自定义类
【发布时间】:2017-01-21 02:56:30
【问题描述】:

我对 Xamarin 很陌生,我正在尝试为 Android/iOS 制作一个应用程序。我正在尝试使用 putExtra 发送一个类,因此我尝试通过添加 ISerializable 接口使该类可序列化,但是每当我尝试这样做时,我都需要实现 Handle() 和 Dispose() 方法。我不确定如何处理这些方法,所以它们有一个 NotImplementedException。

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

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

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

提前致谢。

【问题讨论】:

    标签: c# android xamarin serializable


    【解决方案1】:

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

    希望这会对你有所帮助。

    【讨论】:

    • 这对我不起作用,我已经使用了Java.IO.Serializable,但是我没有像这样输入它,而是像using Java.IO;一样导入了它
    • 将我的代码更改为您的代码不会改变任何东西;我仍然需要 Handle() 和 Dispose() 方法,因为 NotImplementedException 导致我的应用程序崩溃。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多