C#,从抽象基础 class "Parcelable" 中编写 class 的构造函数,它来自 Android 库

C#, write a constructor of a class from an abstract base class "Parcelable", which is from the Android Library

在 C# 中,我需要编写一个 class 的构造函数,如下所示,它从 android 库扩展基础 class,即 "Parcelable"。我收到错误 "Parcelable" 不包含采用 0 个参数的构造函数。

我看到了 C# Error: Parent does not contain a constructor that takes 0 arguments and 但它没有用。 我的Class如下:

...
using Parcel = Android.OS.Parcel;
using Parcelable = Android.OS.Parcelable;

/// <summary>
/// Created by hashmi on 07/07/2015.
/// </summary>
public class Classname: Parcelable
{
    private int id;
    private string name;
    private string address;
    private string latitude;
    private string longitude;

    public Classname(int id, string name, string address, string latitude, string longitude) 
    {
        this.id = id;
        this.name = name;
        this.address = address;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    ...
}

基础代码class Parcelable如下:

using System;
using Android.Runtime;
using Java.Lang;

namespace Android.OS
{
    [Register("android/os/Parcelable", ApiSince = 1, DoNotGenerateAcw = true)]
    public abstract class Parcelable : Java.Lang.Object
    {
        [Register("CONTENTS_FILE_DESCRIPTOR")]
        public const int ContentsFileDescriptor = 1;

        [Obsolete("This constant will be removed in the future version. Use Android.OS.ParcelableWriteFlags enum directly instead of this field.")]
        [Register("PARCELABLE_WRITE_RETURN_VALUE")]
        public const ParcelableWriteFlags ParcelableWriteReturnValue = ParcelableWriteFlags.ReturnValue;
    }
}

基础 class 是一个 Android 图书馆摘要 Class。 我想写一个像上面 "Classname" 这样的构造函数而不修改抽象基础 class 即 "Parcelable".

我已经用下面的代码编辑了原始代码。

using Parcel = Android.OS.Parcel;
using Parcelable = Android.OS.Parcelable;

/// <summary>
/// Created by hashmi on 07/07/2015.
/// </summary>
public class LocationDetail : Parcelable
{

    private int id;
    private string name;
    private string address;
    private string latitude;
    private string longitude;


    public LocationDetail(int id, string name, string address, string latitude, string longitude) 
        : base(id, name, address, latitude, longitude) 
    {
        this.id = id;
        this.name = name;
        this.address = address;
        this.latitude = latitude;
        this.longitude = longitude;
    }
...
}

新错误是"Parcelable"不包含采用 5 个参数的构造函数

从 Android documentation for Parcelable 的外观来看,它似乎没有实现任何构造函数,它实现了一个从 [=14= 构建 Parcelable 的创建者接口].

根据 android 文档(在 Java 中),这是 Parcelable 的典型实现:

 public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

     public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }

         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];
         }
     };

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 }

如您所见,Parcelable 本身不包含构造函数。 稍微讲了讲Android中Parcelables的概念。

我遇到了一些概述如何在 Xamarin 中使用它的指南,其中一个建议您可以像这样实现它:(This guide can be found here)

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

    public int DescribeContents()
    {
        throw new NotImplementedException();
    }

    public void WriteToParcel(Parcel dest, [GeneratedEnum] ParcelableWriteFlags flags)
    {
        throw new NotImplementedException();
    }
}