Xamarin.android Intent传递对象

由于实际工作需要,可能会遇到Intent传递对象的情况,这种情况稍稍麻烦一点,在这里有三种方法可以解决这个问题:

(1) ISerializable接口

(2) IParcelable接口

(3)利用Json对对象进行序列化->反序列,得到原对象(个人推荐第三种,原因:使用简单,一两句话的事情)

这里我将采用第二种方法和第三种方法实验,各位看官可以自行对比。

方法一:利用IParcelable接口

1 新建一个类Student,继承IParcelable接口

[csharp] view plain copy

在CODE上查看代码片派生到我的代码片

  1. public class Student : Java.Lang.Object, IParcelable
  2.     {
  3.         public string Name { getset; }
  4.         public string Sex { getset; }
  5.         public Student()
  6.         {
  7.         }
  8.         private Student(Parcel parcel)
  9.         {
  10.             Sex = parcel.ReadString();
  11.             Name = parcel.ReadString();
  12.         }
  13.        public int DescribeContents()
  14.        {
  15.            return 0;
  16.        }
  17.        public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
  18.        {
  19.            dest.WriteString(Sex);
  20.            dest.WriteString(Name);
  21.        }
  22.         
  23.         #region IParcelable implementation
  24.         // The creator creates an instance of the specified object
  25.         private static readonly GenericParcelableCreator<Student> _creator
  26.             = new GenericParcelableCreator<Student>((parcel) => new Student(parcel));
  27.         [ExportField("CREATOR")]
  28.         public static GenericParcelableCreator<Student> GetCreator()
  29.         {
  30.             return _creator;
  31.         }
  32.         #endregion
  33.     }
  34.     /// <summary>
  35.     /// Generic Parcelable creator that can be used to create objects from parcels
  36.     /// </summary>
  37.     public sealed class GenericParcelableCreator<T> : Java.Lang.Object, IParcelableCreator
  38.         where T : Java.Lang.Object, new()
  39.     {
  40.         private readonly Func<Parcel, T> _createFunc;
  41.         /// <summary>
  42.         /// Initializes a new instance of the <see cref="GenericParcelableCreator{T}"/> class.
  43.         /// </summary>
  44.         /// <param name='createFromParcelFunc'>
  45.         /// Func that creates an instance of T, populated with the values from the parcel parameter
  46.         /// </param>
  47.         public GenericParcelableCreator(Func<Parcel, T> createFromParcelFunc)
  48.         {
  49.             _createFunc = createFromParcelFunc;
  50.         }
  51.         #region IParcelableCreator Implementation
  52.         public Java.Lang.Object CreateFromParcel(Parcel source)
  53.         {
  54.             return _createFunc(source);
  55.         }
  56.         public Java.Lang.Object[] NewArray(int size)
  57.         {
  58.             return new T[size];
  59.         }
  60.         #endregion
  61.     }

2 新建Activity1为测试,在MainActivity中发送对象,代码如下:

[csharp] view plain copy

在CODE上查看代码片派生到我的代码片

  1. protected override void OnCreate(Bundle bundle)
  2.         {
  3.             base.OnCreate(bundle);
  4.             SetContentView(Resource.Layout.Main);
  5.             Button button = FindViewById<Button>(Resource.Id.MyButton);
  6.             button.Click += delegate
  7.             {
  8.                 Student stu=new Student();
  9.                 stu.Name = "Intent对象";
  10.                 stu.Sex = "第五种生物";
  11.                 button.Text = string.Format("{0} clicks!", count++);
  12.                 Intent intent=new Intent(this,typeof(Activity1));
  13.                 intent.PutExtra("mode",  stu);
  14.                 StartActivity(intent);
  15.             };
  16.         }

在Activity中接收对象:

[csharp] view plain copy

在CODE上查看代码片派生到我的代码片

  1. public class Activity1 : Activity
  2.     {
  3.         protected override void OnCreate(Bundle bundle)
  4.         {
  5.             base.OnCreate(bundle);
  6.             var my_class = Intent.GetParcelableExtra("mode"as Student;
  7.         }
  8. }

Debug结果:

方法二:利用Json对对象进行序列化(需自己添加Json库)

参考文章地址:

http://stackoverflow.com/questions/26046668/pass-data-from-one-activity-to-another-in-xamarin-android

最后附上Demo地址:http://download.csdn.net/detail/sinat_26562875/9477273

请大家多多指正~~~

声明:本站内容来源于原创和互联网,尊重作者版权,转载请注明来源网址,欢迎收藏,谢谢!