我是使用Visual Studio在Xamarin中的新手。有一个现有的xamarin android项目,我必须在其中添加一个Jar文件(SDK),所以我创建了一个绑定库,但是它引发了一些我试图通过使用元数据来解决它们的错误。 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 {
}
}
}
由于某种原因,Binding Generator错误地推断了方法实现或返回类型,我不知道,并且查看api.xml似乎是正确的方式:void setSignResult(int p0)。我尝试使用带有相同错误的metadata.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>
我找了另一个接口方法实现,类似于之前提到的与之进行比较,我发现它正确生成了方法,看看:
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 {
}
}
请尽快提供帮助!
答案 0 :(得分:0)
找到答案:
Xamarin绑定生成器(谁在绑定库上工作),正在以错误的方式从java类转换为c#类,它无法执行一些java代码,因此生成器正在创建一个setSignResult作为java的属性结果方法。解决这些问题的方法是使用metadata.xml来处理某些类型的错误,或者在我的情况下,通过创建具有相同自动生成的类名的部分类来实现缺少的方法。由于Binding Generator使用自己的实现创建了DeviceService部分类,因此我创建了另一个实现缺失方法的DeviceService部分类,具有以下实现:
public partial class DeviceService
{
public void SetSignResult(int p)
{
SignResult = p;
}
}
完美无缺!
感谢所有