tags:

views:

46

answers:

1

I have added an event to a user control, and I call the event in the consumer window of the user control,

My question is: what code does the compiler generate when we assign an event handler through the IDE?

So that I can also use something similar to write the event handler at runtime automatically.

I know we can write an event handler e.g my event handler that I write here:

SearchControl.SearchChangedEvent += new RoutedEventHandler(SearchControl_SearchChanged);

The error thrown in this case is that there is no overload matching, so I try to do the same thing that the compiler does through code. How does the compiler automatically know the arguments?

EDIT: Solution.

I found the area of concern where I was confused in this article: http://msdn.microsoft.com/hi-in/magazine/cc785480%28en-us%29.aspx

In "Routed Events overview" section, the author writes:

To see this, go to the constructor for your class, right-click on the InitializeComponent method call, and select Go To Definition from the context menu.The editor will display a generated code file (with a naming convention of .i.g.cs or .i.g.vb) containing the code that is normally generated at compile time.

I found the code behind generated !!

#line 6 "..\..\Window1.xaml"
this.myButton.Click += 
  new System.Windows.RoutedEventHandler(
  this.myButton_Click);

Thanks, for those who were a bit confused with the problem statement. I hope this makes it clear now (it was like I couldn't explain the problem until I found the solution :)

A: 

You fully described your SearchControl_SearchChanged function the second you passed it as a parameter to the delegate RoutedEventHandler. From MSDN, The delegate takes 2 parameters, an object and a RoutedEventArgs, and returns void. That right there is what the IDE uses to build the SearchControl_SearchChanged function header for you automatically.

Blindy
Thanks Blindy,,, I now get what the compiler does......Is there a way to see the class file(if any) that is generated by compilation? Think I have seen something like that, can't recall though...
81967
If you mean ildasm.exe, it's in your `C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin` folder, you run it, select your compiled binary and it will disassemble it for you. I'm sorry if this isn't what you're asking for, your questions are a bit unclear.
Blindy
Thanks Blindy for your help...... I would surely tell you the answer once I come across it..
81967
Hi Blindy, I finally found what I was looking for, just for you information, I was looking for something like SomeFormName.designer.cs, which you get in the window forms, I thought there might be something similar in wpf too, I guess baml is something similar here, but ofcourse it is binary :( , and there are cased I have seen where routedevents are being called just by using the function names, I wonder how is that happening :-)
81967