Xamarin.Android绑定定义使用EnumMethods.xml进行接口的Getter / Setter方法

时间:2016-11-03 17:33:25

标签: android xamarin xamarin.android

我对EnumMethods.xml有疑问。我将它用于某些接口,this映射按预期工作。但是,如果我有另一个扩展原始接口的接口,映射不起作用,我得到以下错误:

"Cannot implicitly convert type 'int' to 'MyEnum'. An explicit conversion exists (are you missing a cast?)"

"Cannot implicitly convert type 'MyEnum' to 'int'. An explicit conversion exists (are you missing a cast?)"

有什么建议吗?

映射如下所示:

<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

1 个答案:

答案 0 :(得分:2)

首先,让我们看一下api.xml的{​​{1}}定义:

Core.IView

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

<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. 项目的p0中将参数重命名为EnumMethods.xml

然后我们可以看到差异:

<强> IVIEW

Core

<强> 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.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);
            }
        }

我们在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_I方法。