Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Android binding Call java Object method

I created java binding library via visual studio extension called Xamarin.GradleBinding. I added ru.rambler.android:swipe-layout:1.0.14 package and while using its SwipeLayout, it all works well. But unfortunately it did not created corresponding C# classes or anything like that. I tried adding package manually but still nothing.

I checked the source on GitHub. SwipeLayout has a public void method reset() without parameters:

public void reset()

I try to call this method from c# with JNIEnv.

IntPtr type = JNIEnv.FindClass("ru/rambler/libs/swipe_layout/SwipeLayout");
IntPtr method = JNIEnv.GetMethodID(type, "reset", "()V");
try
{
    JNIEnv.CallObjectMethod(_swiper.Handle, method);
}
catch (Exception ex)
{
    var s = ex.Message;
}

Type and method are successfully found but calling

JNIEnv.CallObjectMethod(_swiper.Handle, method);

This method crashes the app, it does not even goes into catch block.

Tt must be cause of the _swiper.Handle first parameter. _swiper field is of type ViewGroup since SwipeLayout is derived from ViewGroup. I can't find how to get the pointer of the view to pass that method.

while debugging, when I investigate the _swiper, it seems to be the correct instance of SwipeLayout

enter image description here

like image 818
mister_giga Avatar asked Jan 17 '18 09:01

mister_giga


People also ask

How do you bind an object in xamarin form?

The binding references the source object. To set the data binding, use the following two members of the target class: The BindingContext property specifies the source object. The SetBinding method specifies the target property and source property.

Can I use Java in xamarin?

If you have existing Java functionality that you do not want to convert to C#, you can reuse your existing Java libraries in Xamarin. Android applications via two techniques: Create a Java Bindings Library – Using this approach, you use Xamarin tools to generate C# wrappers around Java types.

What are Android Callable Wrappers ACWs )?

Android Callable Wrappers (ACWs) are required whenever the Android runtime invokes managed code. These wrappers are required because there is no way to register classes with ART (the Android runtime) at runtime. (Specifically, the JNI DefineClass() function is not supported by the Android runtime.}

How do I set binding context in xamarin forms?

In code, two steps are required: The BindingContext property of the target object must be set to the source object, and the SetBinding method (often used in conjunction with the Binding class) must be called on the target object to bind a property of that object to a property of the source object.


1 Answers

I have done a rewrite of the control to C# with Xamarin.Android. It is available here on my GitHub, but I haven't had time to test it yet, so it might have bugs.

But you can try it and potentially fix the bugs I have missed during the rewrite using the original .java source code.

like image 54
Martin Zikmund Avatar answered Oct 04 '22 01:10

Martin Zikmund