Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Android binding defining Getter/Setter Methods using EnumMethods.xml for interface

I have a problem with EnumMethods.xml. I use it for some interface, and this mappings works as expected. But if I have another interface which extends the original one, mappings doesn't work and I get the following error:

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

and

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

Is there any suggestion why ?

Mapping looks like the following:

<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>

EDIT

To clarify. Let's say that the second is called IViewInheritor. So the above errors are inside the IViewInheritorInvoker class. I have the mapping for this class too.

<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

like image 343
nrudnyk Avatar asked Oct 30 '22 17:10

nrudnyk


1 Answers

First, let's take a look at the api.xml definition of the 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>

Okay great, we see the exact parameter names and the return types.

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.

So we need to rename the parameter here to p0 in the EnumMethods.xml of the Core project.

Then we can see the difference:

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

We see that in ICustomView there is a couple of missing casts:

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

Specifically in the n_SetVisibility_I and n_GetVisibility methods respectfully.

like image 186
Jon Douglas Avatar answered Nov 15 '22 07:11

Jon Douglas