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 🙂
You fully described your
SearchControl_SearchChangedfunction the second you passed it as a parameter to the delegateRoutedEventHandler. From MSDN, The delegate takes 2 parameters, anobjectand aRoutedEventArgs, and returnsvoid. That right there is what the IDE uses to build theSearchControl_SearchChangedfunction header for you automatically.