Mono 适用于 Android、Parcelable 和 Android C# - 文档错误

发布于 2024-12-29 16:43:21 字数 1302 浏览 1 评论 0原文

更新:

IParcelable 显然目前无法在 Android 版 Mono 中实现。最后我在类中使用了.NET序列化,然后将序列化数据打包/捆绑在Android特定的代码中,效果很好。它还保持类的跨平台兼容性,这是可取的。

原始问题:

我正在尝试将 Parcelable 实现为 Mono for Android 应用程序中类的一部分,但 Xamarin 的 Parcelable 文档是从 Android 文档复制粘贴的:

http://androidapi.xamarin.com/?link=T%3aAndroid.OS.IParcelable

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();
 }
}

由于该文档是为 Java 编写的,因此对于 C# 来说基本上是错误的。我只是想知道是否有人知道如何将此代码转换为 C#。我在 CREATOR 领域尤其遇到麻烦。

另外,由于我正在尝试编写稍后可以移植到其他平台的代码,那么实现 Parcelable 的最佳方法是什么?我应该使用部分类使其成为类的一部分吗?

UPDATE:

IParcelable apparently cannot currently be implemented in Mono for Android. In the end I used the .NET serialization in the class, and then parceled/bundled the serialized data in the Android-specific code, which works just fine. It also keeps the class cross-platform compatible, which is desirable.

ORIGINAL QUESTION:

I'm trying to implement Parcelable as part of a class in a Mono for Android app, but Xamarin's documentation for Parcelable is copy-pasted from the Android documentation:

http://androidapi.xamarin.com/?link=T%3aAndroid.OS.IParcelable

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();
 }
}

Since that documentation is written for Java, it's basically wrong for C#. I'm just wondering if anyone knows how to convert this code into C#. I'm particularly having trouble with the CREATOR field.

Also, since I'm trying to write code that I can port to other platforms later, what's the best way to implement Parcelable? Should I make it part of the class using partial classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬心 2025-01-05 16:43:21

关于匿名内部类(对我来说是 AIC),我经常忘记的一件事是 AIC 中的“类型”不会直接转换为 C# - 它们是接口,这意味着 C# 的标准是以“I”开头。此外,每个 AIC 必须在 C# 中显式实现。

以下内容有帮助吗?

public class Creator : IParcelableCreator
{


    public Java.Lang.Object CreateFromParcel(Parcel source)
    {
        throw new NotImplementedException();
    }

    public Java.Lang.Object[] NewArray(int size)
    {
        throw new NotImplementedException();
    }

    public IntPtr Handle
    {
        get { throw new NotImplementedException(); }
    }
}

进而:

public class MyParcelable : IParcelable
{


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

    public void WriteToParcel(Parcel dest, int flags)
    {
        throw new NotImplementedException();
    }

    public IntPtr Handle
    {
        get { throw new NotImplementedException(); }
    }
}

One thing I often forget with Anonymous Inner Classes (AIC for me) is that the 'types' in the AIC are not translated Directly into C# - they are interfaces, meaning the standard for C# is to start with an 'I.' Also each AIC must be implemented explicitly in c#.

Does the following help?

public class Creator : IParcelableCreator
{


    public Java.Lang.Object CreateFromParcel(Parcel source)
    {
        throw new NotImplementedException();
    }

    public Java.Lang.Object[] NewArray(int size)
    {
        throw new NotImplementedException();
    }

    public IntPtr Handle
    {
        get { throw new NotImplementedException(); }
    }
}

and then:

public class MyParcelable : IParcelable
{


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

    public void WriteToParcel(Parcel dest, int flags)
    {
        throw new NotImplementedException();
    }

    public IntPtr Handle
    {
        get { throw new NotImplementedException(); }
    }
}
灵芸 2025-01-05 16:43:21

我知道这已经很旧了,但我也遇到了同样的问题。最后我使用 [ExportField("CREATOR")] 让它工作。

所以我最终

public class MyParcelable : Object, IParcelable
{
    public string MyString { get; set; }

    [ExportField("CREATOR")]
    public static MyParcelableCreator InititalizeCreator()
    {
        return new MyParcelableCreator();
    }

    public MyParcelable(string myString)
    {
        MyString = myString;
    }

    public int DescribeContents()
    {
        return 0;
    }

    public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
    {
        dest.WriteString(MyString);
    }

    public class MyParcelableCreator : Object, IParcelableCreator
    {
        public Object CreateFromParcel(Parcel source)
        {
            return new MyParcelable(source.ReadString());
        }

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

希望这对某人有帮助

I know this is old but I've been running into the same issue. In the end I got it working using the [ExportField("CREATOR")].

So I ended up with

public class MyParcelable : Object, IParcelable
{
    public string MyString { get; set; }

    [ExportField("CREATOR")]
    public static MyParcelableCreator InititalizeCreator()
    {
        return new MyParcelableCreator();
    }

    public MyParcelable(string myString)
    {
        MyString = myString;
    }

    public int DescribeContents()
    {
        return 0;
    }

    public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
    {
        dest.WriteString(MyString);
    }

    public class MyParcelableCreator : Object, IParcelableCreator
    {
        public Object CreateFromParcel(Parcel source)
        {
            return new MyParcelable(source.ReadString());
        }

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

Hope this helps someone

幸福还没到 2025-01-05 16:43:21

从资源中,

您必须扩展 Java。 Lang.Object,它避免你处理或处置。

public class GroupMetadata : Java.Lang.Object, IParcelable, IComparable<GroupMetadata> {
        public const int REFRESH = -1;
        public int flPos;
        public int lastChildFlPos;
        public int gPos;
        public long gId;

        private GroupMetadata ()
        {

        }

        public static GroupMetadata Obtain(int flPos, int lastChildFlPos, int gPos, long gId) {
            GroupMetadata gm = new GroupMetadata();
            gm.flPos = flPos;
            gm.lastChildFlPos = lastChildFlPos;
            gm.gPos = gPos;
            gm.gId = gId;
            return gm;
        }

        public int CompareTo(GroupMetadata another) {
            if (another == null) {
                throw new InvalidOperationException();
            }

            return gPos - another.gPos;
        }

        public int DescribeContents() {
            return 0;
        }

        public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags) {
            dest.WriteInt(flPos);
            dest.WriteInt(lastChildFlPos);
            dest.WriteInt(gPos);
            dest.WriteLong(gId);
        }

        public static class Creator : Java.Lang.Object, IParcelableCreator
        {
            public static Java.Lang.Object CreateFromParcel(Parcel source)
            {
                GroupMetadata gm = GroupMetadata.Obtain(
                    source.ReadInt(),
                    source.ReadInt(),
                    source.ReadInt(),
                    source.ReadLong());
                return gm;
            }

            public static Java.Lang.Object[] NewArray(int size)
            {
                return new GroupMetadata[size];
            }
        }
    }

From the resources, Implementing Interfaces

You have to extends Java.Lang.Object, it avoids you to handle or dispose.

public class GroupMetadata : Java.Lang.Object, IParcelable, IComparable<GroupMetadata> {
        public const int REFRESH = -1;
        public int flPos;
        public int lastChildFlPos;
        public int gPos;
        public long gId;

        private GroupMetadata ()
        {

        }

        public static GroupMetadata Obtain(int flPos, int lastChildFlPos, int gPos, long gId) {
            GroupMetadata gm = new GroupMetadata();
            gm.flPos = flPos;
            gm.lastChildFlPos = lastChildFlPos;
            gm.gPos = gPos;
            gm.gId = gId;
            return gm;
        }

        public int CompareTo(GroupMetadata another) {
            if (another == null) {
                throw new InvalidOperationException();
            }

            return gPos - another.gPos;
        }

        public int DescribeContents() {
            return 0;
        }

        public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags) {
            dest.WriteInt(flPos);
            dest.WriteInt(lastChildFlPos);
            dest.WriteInt(gPos);
            dest.WriteLong(gId);
        }

        public static class Creator : Java.Lang.Object, IParcelableCreator
        {
            public static Java.Lang.Object CreateFromParcel(Parcel source)
            {
                GroupMetadata gm = GroupMetadata.Obtain(
                    source.ReadInt(),
                    source.ReadInt(),
                    source.ReadInt(),
                    source.ReadLong());
                return gm;
            }

            public static Java.Lang.Object[] NewArray(int size)
            {
                return new GroupMetadata[size];
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文