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

I have a problem with EnumMethods.xml . I use it for some interface, and this mappings work as expected. But if I have another interface that extends the original, the mappings do not 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?)"

Are there any suggestions as to why?

The display is as follows:

 <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. Say the second is called IViewInheritor. Thus, the above errors are inside the IViewInheritorInvoker class. I have a mapping for this class.

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

+5
source share
1 answer

First, consider the definition of api.xml 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> 

Well, we see the exact parameter names and 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 in 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 there are several missing translations in ICustomView :

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

In particular, in the n_SetVisibility_I and n_GetVisibility .

+2
source

Source: https://habr.com/ru/post/1259245/


All Articles