我尝试制作一个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);
}
}
在尝试制作Parcelable
类之前,我尝试了以下代码
List<TextBlock> blocks = new List<TextBlock>();
Intent drawviewIntent = new Intent(this, typeof(SomeActivity));
drawviewIntent.PutExtra("Blocks", blocks);
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();
}
答案 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"));