How can I write .NET event in C# and attach a matlab callabk to them
1 回表示 (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!