简体   繁体   English

Mono for Android,Parcelable&C# - 文档错误

[英]Mono for Android, Parcelable & C# - Documentation is Wrong

UPDATE: 更新:

IParcelable apparently cannot currently be implemented in Mono for Android . IParcelable 目前显然无法在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. 最后,我在类中使用了.NET序列化,然后在特定于Android的代码中对序列化数据进行了parceled / bundled,这很好用。 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: 我正在尝试将Parcelable实现为Mono for Android应用程序中的一部分,但是Xamarin的Parcelable文档是从Android文档中复制粘贴的:

http://androidapi.xamarin.com/?link=T%3aAndroid.OS.IParcelable 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#. 由于该文档是为Java编写的,因此对C#来说基本上是错误的。 I'm just wondering if anyone knows how to convert this code into C#. 我只是想知道是否有人知道如何将此代码转换为C#。 I'm particularly having trouble with the CREATOR field. 我在CREATOR领域遇到了麻烦。

Also, since I'm trying to write code that I can port to other platforms later, what's the best way to implement Parcelable? 此外,由于我正在尝试编写可以在以后移植到其他平台的代码,实现Parcelable的最佳方法是什么? Should I make it part of the class using partial classes? 我应该使用部分类使其成为课程的一部分吗?

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.' 我经常忘记匿名内部类(AIC对我来说)的一件事是,AIC中的“类型”不会直接转换为C# - 它们是接口,这意味着C#的标准是以“I”开头。 Also each AIC must be implemented explicitly in c#. 此外,每个AIC必须在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(); }
    }
}

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")]. 最后,我使用[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 希望这有助于某人

From the resources, Implementing Interfaces 从资源, 实现接口

You have to extends Java.Lang.Object, it avoids you to handle or dispose. 您必须扩展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];
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM