0
我正在測試如何使用jni4net庫從Java代碼訂閱C#事件,但到目前爲止,我沒有任何已使用的示例工作。我試圖在事件發生時發送一個Body()對象的數組。Jni4net,如何定義在Java中使用C#事件
C#代碼:
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代碼:
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");
}
Java端不使用由C#事件給尚爭論,但我只是想看看它是否被觸發在所有。然而,這會引發以下異常:
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)
被其他網站(以下鏈接)討論的相同的異常,但據我所知定義我的事件時,我沒有使用任何泛型。有誰知道我出錯的地方?
MulticastDelegate for asynchronous callback from Java to DotNet