如何将通用列表对象传递给其他活动到Xamarin Android中的另一个活动?

时间:2018-12-09 13:24:20

标签: android xamarin xamarin.android parcelable

我尝试制作一个Parcelable类,但是WriteToList方法仅使用non generic List,如何将通用List正确地传递给其他活动?

public class ParceableTextBlock : TextBlock, IParcelable
{
    public override IList<IText> Components { get; }

    protected ParceableTextBlock(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference,transfer)
    {

    }

    public int DescribeContents()
    {
        return 0;
    }

    public void WriteToParcel(Parcel dest, [GeneratedEnum] ParcelableWriteFlags flags)
    {
        dest.WriteList(Components);
    }
}

enter image description here

在尝试制作Parcelable类之前,我尝试了以下代码

List<TextBlock> blocks = new List<TextBlock>();
Intent drawviewIntent = new Intent(this, typeof(SomeActivity));
            drawviewIntent.PutExtra("Blocks", blocks);

enter image description here

TextBlock类

[Register("com/google/android/gms/vision/text/TextBlock", DoNotGenerateAcw = true)]
public class TextBlock : Java.Lang.Object, IText, IJavaObject, IDisposable
{
    protected TextBlock(IntPtr javaReference, JniHandleOwnership transfer);

    public virtual Rect BoundingBox { get; }
    public virtual IList<IText> Components { get; }
    public virtual string Language { get; }
    public virtual string Value { get; }
    protected override IntPtr ThresholdClass { get; }
    protected override Type ThresholdType { get; }

    [Register("getCornerPoints", "()[Landroid/graphics/Point;", "GetGetCornerPointsHandler")]
    public virtual Point[] GetCornerPoints();
}

1 个答案:

答案 0 :(得分:1)

您可以添加一个名为Newtonsoft.Json的NuGet Pakeage,然后可以像这样传递列表:

   List<TextBlock> blocks = new List<TextBlock>();
            Intent intent = new Intent(this, typeof(SomeActivity));
            intent.PutExtra("Blocks", JsonConvert.SerializeObject(blocks));
            StartActivity(intent);

,您可以这样获得列表:

  var data = JsonConvert.DeserializeObject<List<TextBlock>>(Intent.GetStringExtra("Blocks"));