How can I write .NET event in C# and attach a matlab callabk to them

조회 수: 1 (최근 30일)
Amir
Amir 2012년 9월 27일
Hi,
I'm working on a project that involves code written in C# and matlab. On the C# side I've declared an event using the standard signature of an Event Handler Delegate as explained in http://www.mathworks.com/help/matlab/matlab_external/working-with-net-events-in-matlab.html I did verify that the event indeed fires (and can be handled from another C# class).
However, ater registering a matlab listener (using the addlistener(obj,<eventName>,<calbbackName>) method), when the event fires, the matlab callback is not triggered.
As I have no problems registering matlab callbacks to other .NET events, I must conclude that somehow I'm using wrong syntax in my C# code.
Can someone please point me to a generic example of C# code that implements an event in a way that Matlab can listen to?

답변 (1개)

Markus Leuthold
Markus Leuthold 2012년 10월 22일
Hi Amir
the handling of .NET events in Matlab is indeed really strange. They are only fired under certain conditions, which don't make any sense to me. An example: Assume you have a winform called Form1 with a combobox called comboBox1 on it. You want a Matlab listener which acts on a change of the combobox selection
classdef TestEvents < handle
properties
f
end
methods
function self=TestEvents
asm=NET.addAssembly('C:\...\test_events.exe');
self.f=test_events.Form1;
self.f.Show;
self.f.Activate;
end
function r=setListener(self)
r=addlistener(self.f.comboBox1,'SelectedValueChanged',@self.myListener);
end
function myListener(self,src,event)
disp('event fired')
end
end
end
Now you show the winform and set the listener by calling
t=TestEvents;
t.setListener;
nothing happens on firing 'SelectedValueChanged'. However, if you call
t=TestEvents;
a=t.setListener;
then the listener is called correctly. So it seems like you need the result of addlistener in the current context, otherwise the event is not handled. This sounds like a bug to me, and I'd like to hear from Mathworks some more details about this behavior. This is on Matlab 2012a
best regards, Kusi

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by