0

When using Xamarin and Java Binding Projects the resulting Binding Project naturally follows the java coding style. I.e. expecting the use of anonymous classes (to implement simple callback interfaces) as parameters. Here is an example of the java code:

// Field
private Player mPlayer;

SDKClass.staticMethod(oneObject, anotherObject, new InitializationObserver() { 
    @Override
    public void onInitialized(SomeClass someClass) { 
        // Calls mPlayer
    }

    @Override
    public void OnError (Java.Lang.Throwable Error)
    {
        Console.WriteLine("Error in initialization: " + Error.Message);

    }
}

The resulting C# code also expect an InitializationObserver as the third parameter. However, since the SDK is closed and I am unable to change the implementation, the only way I can do this is by implementing the interface and its "override" methods in an inner class. This seems very unpractical to me.

In java I can access non-static members from the anonymous class (e.g. the field mPlayer), but afaik this is not possible in C#.

Is there another approach when creating Java Binding Projects in Xamarin that is allows for this? Is there a better way of dealing with the generated classes?

Thank you for helping out. Fred

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Fhl
  • 1,079
  • 1
  • 12
  • 26

1 Answers1

0

I believe there is no other way than to add Player field in your InitializationObserver implementation and pass it as constructor parameter (that's actually what java do behind the scene for anonymous classes). Sometimes when you need only one method you can implement wrapper or extension method (for instance methods) and use lambda and field closure to pass the implementation to java interface implementation.

Artur Shamsutdinov
  • 3,127
  • 3
  • 21
  • 39