gpt4 book ai didi

android - Xamarin.Android 绑定(bind)使用 EnumMethods.xml 定义 Getter/Setter 方法作为接口(interface)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:23 24 4
gpt4 key购买 nike

EnumMethods.xml 有问题。我将它用于某些界面,并且 this映射按预期工作。但是,如果我有另一个扩展原始接口(interface)的接口(interface),则映射不起作用,我会收到以下错误:

“无法将类型‘int’隐式转换为‘MyEnum’。存在显式转换(是否缺少强制转换?)”

“无法将类型‘MyEnum’隐式转换为‘int’。存在显式转换(是否缺少强制转换?)”

有什么建议吗?

映射如下所示:

<mapping jni-interface="path/IView">
<method jni-name="getSmth" parameter="return" clr-enum-type="MyEnum" />
<method jni-name="setSmth" parameter="param" clr-enum-type="MyEnum" />
</mapping>

编辑

澄清一下。假设第二个称为 IViewInheritor。所以上面的错误都在 IViewInheritorInvoker 类里面。我也有这个类的映射。

<mapping jni-interface="path/IViewInheritor">
<method jni-name="getSmth" parameter="return" clr-enum-type="MyEnum" />
<method jni-name="setSmth" parameter="param" clr-enum-type="MyEnum" />
</mapping>

attached bindings project

最佳答案

首先,让我们看一下 Core.IViewapi.xml 定义:

<interface abstract="true" deprecated="not deprecated" final="false" name="IView" static="false" visibility="public">
<method abstract="true" deprecated="not deprecated" final="false" name="getVisibility" native="false" return="int" static="false" synchronized="false" visibility="public">
</method>
<method abstract="true" deprecated="not deprecated" final="false" name="setVisibility" native="false" return="void" static="false" synchronized="false" visibility="public">
<parameter name="p0" type="int">
</parameter>
</method>
</interface>

很好,我们看到了确切的参数名称和返回类型。

1>BINDINGSGENERATOR : warning : [Interface] dom.core.IView in [Method] void setVisibility(int p0) has 'unnamed' parameters

1>BINDINGSGENERATOR : warning BG8A04: <attr path="/api/package[@name='dom.core']/interface[@name='IView']/method[@name='setVisibility']/parameter[@name='visibility']"/> matched no nodes.

所以我们需要在Core项目的EnumMethods.xml中将这里的参数重命名为p0

然后我们就可以看出区别了:

IView:

static Delegate cb_getVisibility;
#pragma warning disable 0169
static Delegate GetGetVisibilityHandler ()
{
if (cb_getVisibility == null)
cb_getVisibility = JNINativeWrapper.CreateDelegate ((Func<IntPtr, IntPtr, int>) n_GetVisibility);
return cb_getVisibility;
}

static int n_GetVisibility (IntPtr jnienv, IntPtr native__this)
{
global::Dom.Core.IView __this = global::Java.Lang.Object.GetObject<global::Dom.Core.IView> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
return (int) __this.Visibility;
}
#pragma warning restore 0169

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

static void n_SetVisibility_I (IntPtr jnienv, IntPtr native__this, int native_p0)
{
global::Dom.Core.IView __this = global::Java.Lang.Object.GetObject<global::Dom.Core.IView> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
global::Android.Views.ViewStates p0 = (global::Android.Views.ViewStates) native_p0;
__this.Visibility = p0;
}
#pragma warning restore 0169

IntPtr id_getVisibility;
IntPtr id_setVisibility_I;
public unsafe global::Android.Views.ViewStates Visibility {
get {
if (id_getVisibility == IntPtr.Zero)
id_getVisibility = JNIEnv.GetMethodID (class_ref, "getVisibility", "()I");
return (global::Android.Views.ViewStates) JNIEnv.CallIntMethod (((global::Java.Lang.Object) this).Handle, id_getVisibility);
}
set {
if (id_setVisibility_I == IntPtr.Zero)
id_setVisibility_I = JNIEnv.GetMethodID (class_ref, "setVisibility", "(I)V");
JValue* __args = stackalloc JValue [1];
__args [0] = new JValue ((int) value);
JNIEnv.CallVoidMethod (((global::Java.Lang.Object) this).Handle, id_setVisibility_I, __args);
}
}

ICustomView:

static Delegate cb_getVisibility;
#pragma warning disable 0169
static Delegate GetGetVisibilityHandler ()
{
if (cb_getVisibility == null)
cb_getVisibility = JNINativeWrapper.CreateDelegate ((Func<IntPtr, IntPtr, int>) n_GetVisibility);
return cb_getVisibility;
}

static int n_GetVisibility (IntPtr jnienv, IntPtr native__this)
{
global::Dom.Common.ICustomView __this = global::Java.Lang.Object.GetObject<global::Dom.Common.ICustomView> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
return __this.Visibility;
}
#pragma warning restore 0169

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

static void n_SetVisibility_I (IntPtr jnienv, IntPtr native__this, int native_value)
{
global::Dom.Common.ICustomView __this = global::Java.Lang.Object.GetObject<global::Dom.Common.ICustomView> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
global::Android.Views.ViewStates value = (global::Android.Views.ViewStates) native_value;
__this.Visibility = value;
}
#pragma warning restore 0169

IntPtr id_getVisibility;
IntPtr id_setVisibility_I;
public unsafe global::Android.Views.ViewStates Visibility {
get {
if (id_getVisibility == IntPtr.Zero)
id_getVisibility = JNIEnv.GetMethodID (class_ref, "getVisibility", "()I");
return JNIEnv.CallIntMethod (((global::Java.Lang.Object) this).Handle, id_getVisibility);
}
set {
if (id_setVisibility_I == IntPtr.Zero)
id_setVisibility_I = JNIEnv.GetMethodID (class_ref, "setVisibility", "(I)V");
JValue* __args = stackalloc JValue [1];
__args [0] = new JValue ((int) value);
JNIEnv.CallVoidMethod (((global::Java.Lang.Object) this).Handle, id_setVisibility_I, __args);
}
}

我们看到在 ICustomView 中有几个缺失的转换:

(global::Android.Views.ViewStates) and (int)

具体在n_SetVisibility_In_GetVisibility方法中恭敬。

关于android - Xamarin.Android 绑定(bind)使用 EnumMethods.xml 定义 Getter/Setter 方法作为接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40407874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com