Interface Callback in Xamarin
Asked Answered
D

2

1

I am trying to write a interface callback from a java library inside Xamarin using c#, Below is the code snippet,

Connector.getInstance().login(email, new ILoginCallback() {
@Override
public void onSuccess(long heloUserId) {
/* call the required Activity for successful login */
}
@Override
public void onPinverification() {
/*call the Pin verification activity to validate pin */
}
@Override
public void onFailure(String description) {
/*show the toast msg for failure*/
}
});

I am unable to do interface callback in c# which blocked me to get the success or failure result.

Any Help appreciated.

Dombrowski answered 10/12, 2017 at 11:33 Comment(0)
C
1

Implement your interface on a class that inherits from Java.Lang.Object:

public class MyLoginCallback : Java.Lang.Object, ILoginCallback
{
   // implement the interface
}

And then use that C# class that also now has a Android Callable Wrapper (ACW) as your callback instance.

Something like:

Connector.Instance.Login(email, new MyLoginCallback();

Re: Android Callable Wrappers

Couloir answered 10/12, 2017 at 11:49 Comment(9)
Thanks for your response. Unfortunately but the code is still not working. Can you please elaborate more.Dombrowski
I did something like this, public partial class HeloLogin : ContentPage { private void OnRegister(object sender, EventArgs e) { Connector.Instance.Login(Email.Text, new XYZ()); } } public class XYZ : Java.Lang.Object, ILoginCallback { public void Dispose() { } public void OnFailure(string p0) { } public void OnPinverification() { } public void OnSuccess(long p0) { } }Dombrowski
@Dombrowski That is not telling me what is not "working"Couloir
I placed breakpoints on each method. but nothing gets hit after callback line executed.Dombrowski
@Dombrowski You would need to debug whatever Connector.Instance.Login does... I would have no idea what it does or what Java library is bound.Couloir
Can you please skype on anuragc64 ?Dombrowski
@SushiHangover, I implemented the interface as you said. It throws error when the callback in Android binding trying to execute my callback as System.NotSupportedException Message=Unable to activate instance of type myapppackage.Droid.MyCallback from native handle 0xAxyz (key_handle 0xyue736)Trout
@ThamaraiT post a new question with your codeCouloir
@SushiHangover, question is already here. Kindly refer this.Trout
T
1

Implement your interface as like below

public class MyLoginCallback : Java.Lang.Object, ILoginCallback, ISerializable
{    
   public MyLoginCallback()
   { }

   public MyLoginCallback(IntPtr intPtr, Android.Runtime.JniHandleOwnership handleOwnership)
   { }

   // implement the interface methods here
}
Trout answered 11/10, 2021 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.