具有属性List <object>的Parcelable类的C#示例

时间:2017-12-11 16:59:33

标签: c# android xamarin xamarin.android parcelable

有人可以提供一个简单的C#示例,使用Parcelable方法通过Intent传递对象,其中对象包含另一个对象的List吗?我真的只需要弄清楚如何写/读列表。我测试了一些成功通过OtherObject的代码,因此应该为Parcelable正确设置,与FromActivity和ToActivity中的代码相同。以下是我正在寻找的一些示例代码:

Public Class MyObject : Object, IParcelable
{
    public int myId {get; private set;}
    public List<OtherObject> myObjectList {get; set;}

    public MyObject(int myId)
    {
        //This constructor called on the item being passed through intent
        this.myId = myId;
        RepoClass _repoClass = new RepoClass();
        this.myObjectList = _repoClass.BuildList(myId).ToList();
    }

    public MyObject(Parcel parcel)    //Passed from Creator object
    {
        this.myId = parcel.ReadInt();
        this.myObjectList = new List<OtherObject>

        //NOT SURE HOW TO READ -- tried the following:
        //parcel.ReadList(myObjectList, new OtherObject().Class.ClassLoader);        //Not Working
        //parcel.ReadTypedList(myObjectList, new OtherObjectParcelableCreator();     //Not Working
    }
    ...
    public void WriteToParcel(Parcel dest, [GenerateEnum] ParcelableWriteFlags flags)
    {
        dest.WriteInt(myId);
        //NOT SURE HOW TO WRITE LIST -- tried the following, but don't know how to test if either is working since I am not sure how to properly read
        //dest.WriteList(myObjectList);
        //dest.WriteTypedList(myObjectList);
    }

    ...
}

0 个答案:

没有答案