Mono 适用于 Android、Parcelable 和 Android C# - 文档错误
更新:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于匿名内部类(对我来说是 AIC),我经常忘记的一件事是 AIC 中的“类型”不会直接转换为 C# - 它们是接口,这意味着 C# 的标准是以“I”开头。此外,每个 AIC 必须在 C# 中显式实现。
以下内容有帮助吗?
进而:
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?
and then:
我知道这已经很旧了,但我也遇到了同样的问题。最后我使用 [ExportField("CREATOR")] 让它工作。
所以我最终
希望这对某人有帮助
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
Hope this helps someone
从资源中,
您必须扩展 Java。 Lang.Object,它避免你处理或处置。
From the resources, Implementing Interfaces
You have to extends Java.Lang.Object, it avoids you to handle or dispose.