天天看點

Xamarin.android Intent傳遞對象

由于實際工作需要,可能會遇到Intent傳遞對象的情況,這種情況稍稍麻煩一點,在這裡有三種方法可以解決這個問題:

(1) ISerializable接口

(2) IParcelable接口

(3)利用Json對對象進行序列化->反序列,得到原對象(個人推薦第三種,原因:使用簡單,一兩句話的事情)

這裡我将采用第二種方法和第三種方法實驗,各位看官可以自行對比。

方法一:利用IParcelable接口

1 建立一個類Student,繼承IParcelable接口

public class Student : Java.Lang.Object, IParcelable
    {
        public string Name { get; set; }

        public string Sex { get; set; }

        public Student()
        {
        }

        private Student(Parcel parcel)
        {
            Sex = parcel.ReadString();
            Name = parcel.ReadString();
        }

       public int DescribeContents()
       {
           return 0;
       }

       public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
       {
           dest.WriteString(Sex);
           dest.WriteString(Name);
       }
        
		#region IParcelable implementation

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

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

		#endregion
    }

    /// <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="GenericParcelableCreator{T}"/> 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
    }
           

2 建立Activity1為測試,在MainActivity中發送對象,代碼如下:

protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            button.Click += delegate
            {
                Student stu=new Student();
                stu.Name = "Intent對象";
                stu.Sex = "第五種生物";
                button.Text = string.Format("{0} clicks!", count++);
                Intent intent=new Intent(this,typeof(Activity1));
                intent.PutExtra("mode",  stu);
                StartActivity(intent);
            };

        }
           

在Activity中接收對象:

public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            var my_class = Intent.GetParcelableExtra("mode") as Student;
          
        }
}

           

Debug結果:

Xamarin.android Intent傳遞對象

方法二:利用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

請大家多多指正~~~

繼續閱讀