2016-09-13 71 views
0

我對Xamarin非常新,我正在嘗試爲Android/iOS創建應用程序。我試圖通過putExtra發送一個類,所以我嘗試通過添加ISerializable接口來創建類Serializable,但每當我嘗試這樣做時,我需要實現一個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(); 
    } 
} 

這個類是在(便攜式)項目,而不是在.Droid項目,因爲我還需要它在iOS中。

有人能告訴我我做錯了什麼,或者可能提出一個替代方案嗎?

在此先感謝。

回答

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/

希望這會幫助你。

+0

這對我不起作用,我已經使用'Java.IO.Serializable',但不是像這樣把它像'using Java.IO'一樣導入,' –

+0

@DennisvanOpstal你會得到任何錯誤嗎? – Ironman

+0

將我的代碼更改爲您的代碼不會改變任何內容;我仍然需要Handle()和Dispose()方法,這些方法使得我的應用程序因爲NotImplementedException而崩潰。 –