How to convert (cast) Java.Lang.Object to c# Interface

ArvisArvis USMember ✭✭
edited April 2015 in Xamarin.Android

I can successfully bind jar file library and bulded binding project.
But the needed object can be instantiated only by static factory function:
NavAppClientFactory.Make(ctx, errorCallback);
And the problem is that method returns Java.Lang.Object that supposed to be INavAppClient interface object (at least it was so in java).
So the question is how can i get (cast) INavAppClient out of that Java.Lang.Object to access all interface functions?

        public static INavAppClient NavClient { get; private set; }

        public static INavAppClient CreateNavAppClient(Context ctx)
        {
            if (NavClient != null) return NavClient;
            try
            {
                NavClient = (INavAppClient)NavAppClientFactory.Make(ctx, errorCallback);  // Throws InvalidCastException!
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
            return NavClient;
        }

From the genereted wraper code signatures of objects:

[Register ("com/tomtom/navapp/NavAppClient", "", "Com.Tomtom.Navapp.INavAppClientInvoker")]
public partial interface INavAppClient : IJavaObject
    { ... }

[global::Android.Runtime.Register ("com/tomtom/navapp/NavAppClient$Factory", DoNotGenerateAcw=true)]
public partial class NavAppClientFactory : global::Java.Lang.Object {

    [Register ("make", "(Landroid/content/Context;Lcom/tomtom/navapp/ErrorCallback;)Lcom/tomtom/navapp/NavAppClient;", "")]
    public static global::Java.Lang.Object Make (global::Android.Content.Context p0, global::Com.Tomtom.Navapp.IErrorCallback p1)
    { ... }
}

Posts

  • ArvisArvis USMember ✭✭

    Somehow miss this JavaCast function!

    NavClient = NavAppClientFactory.Make(ctx, errorCallback).JavaCast<INavAppClient>();

  • BalTaZaRRBalTaZaRR Member

    @Arvis said:
    Somehow miss this JavaCast function!

    NavClient = NavAppClientFactory.Make(ctx, errorCallback).JavaCast<INavAppClient>();

    Hello Arvis,

    I see you are also one of the few who use the navsdk of TomTom with Xamarin. Is it possible that I can get the jar binding project from you.
    Would be very nice!!!

Sign In or Register to comment.