简体   繁体   中英

Jni4net, How to define C# events for use in Java

I'm testing out how C# events can be subscribed to from Java code using the jni4net library, but so far have none of the examples I've made worked. I'm trying to send over an array of Body() objects when the event is raised.

C# code:

public class LibEventHandler
{

    // Event for updating the body frame
    public delegate void UpdateBodyFrameEventHandler(object source, BodyDataEventArgs e);
    public event UpdateBodyFrameEventHandler UpdateBody;
    protected virtual void OnUpdateBody(BodyDataEventArgs bodies)
    {
        UpdateBodyFrameEventHandler handler = UpdateBody;
        if (handler != null)
           handler(this, bodies);
    }
    public void raiseUpdateBodyEvent(Body[] bodies)
    {
        OnUpdateBody(new BodyDataEventArgs() { Bodies = bodies });

    }

}

/// <summary>
/// A class extending EventArgs. Specifies the type Body[] as the event argument for the UpdateBody event
/// </summary>
public class BodyDataEventArgs : EventArgs
{
    public Body[] Bodies { get; set; }
}

Java code:

public Start(){
    //initialise jni4net
    try {

        Bridge.setVerbose(true);
        Bridge.init();

        Bridge.LoadAndRegisterAssemblyFrom(new File("testlib.j4n.dll"));

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //create a instance of the library's main class and get the object that has all the events
    LibMain lib = new LibMain();
    LibEventHandler evHandler = lib.getEventHandler();

    //subscribe to the event UpdateBody
    evHandler.addUpdateBody(new EventHandler(){
        @Override
        public void Invoke(system.Object sender, EventArgs e){
            Hello();
        }
    });
}


private void Hello(){
    System.out.println("Hello World! triggered by c# event");
}

The java side doesn't use the arguments given by the C# event yet, but i just wanted to see if it gets triggered at all. This however throws the following exception:

    Exception in thread "main" System.ArgumentNullException: Value cannot be null.
Parameter name: method
   at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
   at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
   at net.sf.jni4net.utils.Convertor.StrongJ2CpDelegate[TRes](JNIEnv env, JniLocalHandle obj)
   at testlib.__LibEventHandler.UpdateBody0(IntPtr __envp, JniLocalHandle __obj, JniLocalHandle value)
    at testlib.LibEventHandler.addUpdateBody(Native Method)
    at testing.Start.<init>(Start.java:35)
    at testing.Testing.main(Testing.java:24)

The same exception was discussed on another website (link below), but as far as I know I'm not using any generics when defining my event. Does anyone know where I've gone wrong?

MulticastDelegate for asynchronous callback from Java to DotNet

As it turned out, JNI4NET does NOT support delegate events. So the solution was to use a interface event system with listeners for the java side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM