簡體   English   中英

錯誤CS0535類未實現接口成員

[英]error CS0535 Class does not implement interface member

我是使用Visual Studio的Xamarin的新手。 有一個現有的xamarin android項目,我不得不在其中添加一個Jar文件(SDK),因此我創建了一個綁定庫,但是它拋出了一些錯誤,我試圖通過使用Metadata.xml來解決它們,其中一個是如下:

'DeviceService'未實現接口成員'IDeviceService.SetSignResult(int)'

搜索api.xml我發現了這一點:

<class abstract="false" deprecated="not deprecated" extends="android.app.Service" extends-generic-aware="android.app.Service" final="false" name="DeviceService" static="false" visibility="public">
<implements name="com.company.deviceService.IDeviceService" name-generic-aware="com.company.deviceService.IDeviceService">
</implements>
<constructor deprecated="not deprecated" final="false" name="DeviceService" static="false" type="com.company.deviceService.DeviceService" visibility="public">
</constructor>
...
<method abstract="false" deprecated="not deprecated" final="false" name="setSignResult" native="false" return="void" static="false" synchronized="false" visibility="public">
<parameter name="p0" type="int">
</parameter>
</method>

<method abstract="false" deprecated="not deprecated" final="false"   name="getSignResult" native="false" return="int" static="false" synchronized="false" visibility="public">
</method>
...
</class>

在接口中有這樣的方法:

IntPtr id_setSignResult_I;
        public unsafe void SetSignResult (int p0)
        {
            if (id_setSignResult_I == IntPtr.Zero)
                id_setSignResult_I = JNIEnv.GetMethodID (class_ref, "setSignResult", "(I)V");
            JValue* __args = stackalloc JValue [1];
            __args [0] = new JValue (p0);
            JNIEnv.CallVoidMethod (Handle, id_setSignResult_I, __args);
        }

但是,當我搜索生成的類DeviceService.cs時,發現了這一點:

static Delegate cb_setSignResult_I;
#pragma warning disable 0169
        static Delegate GetSetSignResult_IHandler ()
        {
            if (cb_setSignResult_I == null)
                cb_setSignResult_I = JNINativeWrapper.CreateDelegate ((Action<IntPtr, IntPtr, int>) n_SetSignResult_I);
            return cb_setSignResult_I;
        }

        static void n_SetSignResult_I (IntPtr jnienv, IntPtr native__this, int p0)
        {
            global::com.company.deviceService.DeviceService __this = global::Java.Lang.Object.GetObject<global::com.company.deviceService.DeviceService> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
            __this.SignResult = p0;
        }
#pragma warning restore 0169

        static IntPtr id_getSignResult;
        static IntPtr id_setSignResult_I;

        public virtual unsafe int SignResult {
            // Metadata.xml XPath method reference: path="/api/package[@name='com.company.deviceService']/class[@name='DeviceService']/method[@name='getSignResult' and count(parameter)=0]"
            [Register ("getSignResult", "()I", "GetGetSignResultHandler")]
            get {
                if (id_getSignResult == IntPtr.Zero)
                    id_getSignResult = JNIEnv.GetMethodID (class_ref, "getSignResult", "()I");
                try {

                    if (GetType () == ThresholdType)
                        return JNIEnv.CallIntMethod  (Handle, id_getSignResult);
                    else
                        return JNIEnv.CallNonvirtualIntMethod  (Handle, ThresholdClass, JNIEnv.GetMethodID (ThresholdClass, "getSignResult", "()I"));
                } finally {
                }
            }
            // Metadata.xml XPath method reference: path="/api/package[@name='com.company.deviceService']/class[@name='DeviceService']/method[@name='setSignResult' and count(parameter)=1 and parameter[1][@type='int']]"
            [Register ("setSignResult", "(I)V", "GetSetSignResult_IHandler")]
            set {
                if (id_setSignResult_I == IntPtr.Zero)
                    id_setSignResult_I = JNIEnv.GetMethodID (class_ref, "setSignResult", "(I)V");
                try {
                    JValue* __args = stackalloc JValue [1];
                    __args [0] = new JValue (value);

                    if (GetType () == ThresholdType)
                        JNIEnv.CallVoidMethod  (Handle, id_setSignResult_I, __args);
                    else
                        JNIEnv.CallNonvirtualVoidMethod  (Handle, ThresholdClass, JNIEnv.GetMethodID (ThresholdClass, "setSignResult", "(I)V"), __args);
                } finally {
                }
            }
        }

出於某種原因,綁定生成器錯誤地推斷了方法的實現或返回類型,我不知道,並且在api.xml中進行查找似乎是正確的方法:void setSignResult(int p0)。 我試圖通過使用具有相同錯誤的metas.xml來修改行為:

  <attr path="/api/package[@name='Com.Company.deviceservice']/class[@name='DeviceService']/method[@name='SetSignResult' 
    and count(parameter)=1
    and parameter[1][@type='int']]/parameter[1]"
    name="managedReturn">Java.Lang.Void</attr>

我尋找了另一個Interface方法的實現,類似於之前提到的與之比較的結果,我發現它正確地生成了該方法,請看一下:

static IntPtr id_bcrSymbologyToText_I;
        [Register ("bcrSymbologyToText", "(I)Ljava/lang/String;", "GetBcrSymbologyToText_IHandler")]

        public virtual unsafe string BcrSymbologyToText (int p0)
        {
            if (id_bcrSymbologyToText_I == IntPtr.Zero)
                id_bcrSymbologyToText_I = JNIEnv.GetMethodID (class_ref, "bcrSymbologyToText", "(I)Ljava/lang/String;");
            try {
                JValue* __args = stackalloc JValue [1];
                __args [0] = new JValue (p0);

                if (GetType () == ThresholdType)
                    return JNIEnv.GetString (JNIEnv.CallObjectMethod  (Handle, id_bcrSymbologyToText_I, __args), JniHandleOwnership.TransferLocalRef);
                else
                    return JNIEnv.GetString (JNIEnv.CallNonvirtualObjectMethod  (Handle, ThresholdClass, JNIEnv.GetMethodID (ThresholdClass, "bcrSymbologyToText", "(I)Ljava/lang/String;"), __args), JniHandleOwnership.TransferLocalRef);
            } finally {
            }
        }

請盡快需要您的幫助!

找到答案:

Xamarin綁定生成器(在綁定庫上工作的人)正在以錯誤的方式將Java類無法轉換的某些Java代碼轉換為c#類,因此生成器將setSignResult創建為java方法的屬性結果。 解決這些問題的方法是將meta.xml用於某種類型的錯誤,否則,通過創建具有相同自動生成的類名的部分類來實現丟失的方法。 由於綁定生成器使用其自己的實現創建了DeviceService部分類,因此我創建了另一個DeviceService部分類,該類通過以下實現來實現缺少的方法:

public partial class DeviceService
    {
        public void SetSignResult(int p)
        {
            SignResult = p;
        }
}

完美的作品!

謝謝大家

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM