C#相当于Xamarin.Android的Java代码段

时间:2017-05-25 18:47:04

标签: android xamarin.android ormlite sqlite-net

我正在尝试将使用ORMLite的Android代码库转换为使用SQLite-net PCL nuget包的Xamarin.Android的C#等效代码。

Java:

public abstract class DTAbstractEntity {


}

import entities.DTAbstractEntity;
import com.j256.ormlite.field.DatabaseField;

public abstract class DTAbstractModelEntity<T extends DTAbstractEntity> extends DTAbstractEntity {

    public final static String ID_FIELD_NAME = "uuid";

    @DatabaseField(id = true, canBeNull = false, columnName = ID_FIELD_NAME)
    protected String uuid;

    @DatabaseField
    protected String name;

    @DatabaseField
    protected String path;

    @DatabaseField
    protected boolean completeResponse;

    /* GETTERS */
    public String getUuid() { return uuid; }

    public String getName() {
        return name;
    }

    public String getPath() {
        return path;
    }

    public boolean isCompleteResponse() {
        return completeResponse;
    }
}

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable
public class DTProfile extends DTAbstractModelEntity {

    @DatabaseField
    private int unreadCount;

    @DatabaseField
    private boolean linkedInAuthorised;

    @DatabaseField
    private boolean hasSubscriptions;

    @DatabaseField(foreign=true, foreignAutoCreate = true, foreignAutoRefresh = true)
    private DTLocale localePreference;

    @DatabaseField
    private String loginType;  //email or social login provider ie twitter, google, facebook etc

    /* CONSTRUCTOR */
    public DTProfile() {
        //constructor stub - needed by ORMLite
    }

     /* GETTERS */

    public int getUnreadCount() {
        return unreadCount;
    }

    public boolean isLinkedInAuthorised() {
        return linkedInAuthorised;
    }

    public boolean isHasSubscriptions() {
        return hasSubscriptions;
    }

    public DTLocale getLocalePreference() {
        return localePreference;
    }

    public String getLoginType() { return loginType; }

}

任何人都可以在此提供指导,将Java代码迁移到C#等价物

1 个答案:

答案 0 :(得分:0)

我检查了您的上一个问题:What is the equivalent package of ORMLite used in case of Xamarin.Android application,建议使用SQLite代替OrmLite的答案,但您没有标记答案,所以我想你不要我想替换这个OrmLite包。

然后,您可以使用Xamarin的Binding Library从Android .JAR文件为Xamarin.Android创建Xamarin.Android Java Bindings库。

我测试了下载OrmLite android 5.0 jar package并按照official guide这里创建了一个dll,工作正常。

enter image description here

enter image description here

如果你想知道如何使用SQLite-net PCL,你可以查看:Create a Database with SQLite,你的代码看起来只是为了在数据库中创建表,这个doc也提供了创建,连接数据库和插入,从数据库中获取数据。这是sample code