c# - Attach an EventHandler Defined in One Class to an Element Defined in a Different Class -
searched around syntax couldn't find anything. have event handler defined in main file , want attach element creating in different class. know syntax adding event handler inside class is
element.previewmousedown += eventhandlername; but i'm not sure syntax adding handler that's in seperate class. tried
element.previewmousedown += mainwindow.eventhandlername; but invalid. appreciated
you need reference instance want receive handler. cannot refer class (unless handler static method):
void main() { var = new a(); var b = new b(); a.event += b.handler; // reference instance method } class { public event eventhandler event; } class b { public void handler(object sender, eventargs args) { } } if have static handler, like:
void main() { var = new a(); a.event += b.handler; // reference static method } class { public event eventhandler event; } class b { public static void handler(object sender, eventargs args) { } }
Comments
Post a Comment