在C#中的Android活动之间传递自定义对象

时间:2013-09-25 06:42:46

标签: c# android xamarin.android xamarin

我正在使用Xamarin.Android在VS2012中创建一个Android应用程序。我在主屏幕中显示自定义列表。当用户点击列表项时,我需要从这个Main活动传递一个自定义对象(带有ID,String,String,String属性)。

有人可以帮我举个例子吗?

编辑:

我已在other question

中尝试过解决方案

但问题是我在异常之下:

这就是我在第二个活动中提取

的方法
InsuranceReminderBO i = (InsuranceReminderBO)Intent.GetSerializableExtra("SelectedItemID");

我为空

并在第一个活动设置中,它是这样的:

Intent intent = new Intent(this, typeof(ReminderDetails));

intent.PutExtra("SelectedItemID", selectedInsurance);

StartActivity(typeof(ReminderDetails));

其中class InsuranceReminderBO定义为

public class InsuranceReminderBO : Java.Lang.Object, Java.IO.ISerializable

我也尝试过使用IParcelable,但我得到了错误Creator is not defined in ICreator or Creator

在CustomObject上实现Iparcelable

'public class InsuranceReminderBO : Java.Lang.Object, IParcelable
    {
        public InsuranceReminderBO()
        {
        }

        #region Objects and Properties
        private int id;
        private String strCompanyName;
        private String strPremiumAmount;
        private String stDueDate;

        public int ID
        {
            get { return this.id; }
            set { this.id = value; }
        }

        public String Company_Name
        {
            get { return this.strCompanyName; }
            set { this.strCompanyName = value; }
        }

        public String Premium_Amount
        {
            get { return this.strPremiumAmount; }
            set { this.strPremiumAmount = value; }
        }

        public String Due_Date
        {
            get { return this.stDueDate; }
            set { this.stDueDate = value; }
        }
        #endregion

        #region IParcelable implementation

        // The creator creates an instance of the specified object
        private static readonly GenericParcelableCreator<InsuranceReminderBO> _creator 
            = new GenericParcelableCreator<InsuranceReminderBO>((parcel) => new InsuranceReminderBO(parcel));

        [ExportField("CREATOR")]
        public static GenericParcelableCreator<InsuranceReminderBO> GetCreator()
        {
            return _creator;
        }

        // Create a new SelectListItem populated with the values in parcel
        private InsuranceReminderBO(Parcel parcel)
        {
            ID = parcel.ReadInt();
            Company_Name = parcel.ReadString();
            Premium_Amount = parcel.ReadString();
            Due_Date = parcel.ReadString();
        }

        public int DescribeContents()
        {
            return 0;
        }

        // Save this instance's values to the parcel
        public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
        {
            dest.WriteInt(ID);
            dest.WriteString(Company_Name);
            dest.WriteString(Premium_Amount);
            dest.WriteString(Due_Date);
        }

        // Closest to the 'Java' way of implementing the creator
        /*public sealed class SelectListItemCreator : Java.Lang.Object, IParcelableCreator
        {
            public Java.Lang.Object CreateFromParcel(Parcel source)
            {
                return new SelectListItem(source);
            }

            public Java.Lang.Object[] NewArray(int size)
            {
                return new SelectListItem[size];
            }
        }*/

        #endregion
    }

#region GenericParcelableCreator
    /// <summary>
    /// Generic Parcelable creator that can be used to create objects from parcels
    /// </summary>
    public sealed class GenericParcelableCreator<T> : Java.Lang.Object, IParcelableCreator
        where T : Java.Lang.Object, new()
    {
        private readonly Func<Parcel, T> _createFunc;

        /// <summary>
        /// Initializes a new instance of the <see cref="ParcelableDemo.GenericParcelableCreator`1"/> class.
        /// </summary>
        /// <param name='createFromParcelFunc'>
        /// Func that creates an instance of T, populated with the values from the parcel parameter
        /// </param>
        public GenericParcelableCreator(Func<Parcel, T> createFromParcelFunc)
        {
            _createFunc = createFromParcelFunc;
        }

        #region IParcelableCreator Implementation

        public Java.Lang.Object CreateFromParcel(Parcel source)
        {
            return _createFunc(source);
        }

        public Java.Lang.Object[] NewArray(int size)
        {
            return new T[size];
        }
        #endregion
    }
#endregion'

我将对象置于意图中

InsuranceReminderBO selectedInsurance = listOfInsurance[e.Position];
Intent intent = new Intent(this, typeof(ReminderDetails));
            intent.PutExtra("SelectedItem", selectedInsurance);

阅读第二项活动

InsuranceReminderBO i = (InsuranceReminderBO)Intent.GetParcelableExtra("SelectedItem");

但是让我为空。

3 个答案:

答案 0 :(得分:2)

要在servicestack.text解决方案上涂尾,您只需下载android DLL并将其引用到您的解决方案中即可。您可以使用它并将其添加到您的解决方案中,单独构建它作为替代方案。 https://github.com/ServiceStack/ServiceStack.Text/tree/master/src/ServiceStack.Text.Android

另外,我使用了几种方法来回转换可能有用的项目,试试

static public string ToJSON(this object item)
{
  var myval = JsonSerializer.SerializeToString(item);
  return myval;
}
static public T FromJSON<T>(string code)
{
   var item = JsonSerializer.DeserializeFromString<T>(code);
   return item;
}

答案 1 :(得分:1)

有一篇关于在Xamarin here中使用IParcelable的文章。

就个人而言,我总是将序列化为JSON,传递一个字符串,并在另一端反序列化。

如果您正在使用我喜欢的ServiceStack.Text,您可以执行以下操作:

intent.PutExtra("SelectedItemId", JsonSerializer.SerializeToString(selectedInsurance));

另一方面:

var insurance = JsonSerializer.DeserializeFromString<InsuranceReminderBO>(Intent.GetStringExtra("SelectedItemId"))

无需实施Java.Lang.Object, Java.IO.ISerializable

答案 2 :(得分:0)

在Google上进行了大量搜索之后,我找到了一个解决方案here

基本上我使用了StartActivity(intent);