Thursday, 2 August 2012

Post/Email-3: Events in .NET

Today, a friend of mine asked a question on facebook:
Any body knows why wouldn't the selectionChanged event be fired?
I'm trying to change the text of a textBlock once I scroll to the next item in a pivot page
When I came into the scene, so to speak, another person had supplied the fix for the issue like so:
hmm in xaml u have to call your function SelectionChanged="thisPivot_SelectionChanged" and your textblock gets changed...

Then I started talking to my friend, and here’s how our conversation went:

So, you fixed the issue!

I had to call the event in the xaml code too.
I didn't know about that

"call the event in xaml"?
what do you mean by that?

hmmm, yes
this line was missing: SelectionChanged="thisPivot_SelectionChanged"

you can't "call an event from xaml"!
here you "wire up" the event "SelectionChanged" to the event handler "thisPivot_SelectionChanged"
and you can do this from code or from xaml

"wire up"
that's new for me!

what did you do before this?

just defined the handler

ah, ok. and you just expected it to magically know when to fire (or rather when to be called)

yes yes :P

would you like me to give you a little explanation about events in .NET?

yeah, of course!

ok, so could you first tell me what do YOU know about events in .NET?

I know the concept
events are fired along the application
handlers must be defined to handle a specific event we need to handle to achieve some task in our app

ok, that's good

Now, here’s my simple overview on events in .NET:

Events, as their name suggests, are a mechanism in .NET that allows for an object to inform (notify) of something that happened to it (or in it) to a subscribing party (some other object that "declares an interest" in such a "thing"). An example of this is a "Pivot" object that has a "SelectionChanged" event, which means that it can notify subscribing objects when the selected pivot item changes.
this is allowed in a "loosely coupled" manner, meaning that although the subscriber has to know on which object it is subscribing, but the object that defines and fires the event doesn't have to know who is "listening" to the event

Moreover, the subscriber doesn't have to "wait" for the event. It just declares that it is interested in it, by providing an "event handler" (and in this case we say that it is "listening" to the event). When an event gets fired, all listening objects are notified, and their "event handlers" are called.

Each event has a corresponding "signature" that its handlers have to follow. A SelectionChanged event can be declared like so:
public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
which means that any handler for this event has to have the following signature:
void Event_Handler_Name(object sender, SelectionChangedEventArgs args)
So, we have a couple of parameters:
a “sender” object, which would be the object that fired the event;
and an “event argument”, which is an object that generally derives from "EventArgs”, and is used to send relevant information about the event to the subscribing objects.
the syntax to subscribe to an event is like so:
(e.g. pivot.SelectionChanged+=thisPivot_SelectionChanged;)
it's what happens when you say Event="EventHandler" in xaml
At this point, my friend asked:
where to write it in the code?
in the constructor?

yeah, you can put it in the constructor if you want to make it available since the creation of the object, but you can do it inside any method you want

let's say that you don't want to start subscribing to the event only if the user clicks a certain button. in that case, you put it inside the event handler of the button click event (subscribing to an event, in the event handler of another event. not confusing at all  )
now, to "fire" an event, the object that defines it does something like this:
   handler(this, new SelectionChangedEventArgs{SelectedItem=selected});
so you notice a couple of things here.
first, the event is "copied" to a variable. This is done so that you don't get weird behavior if some object subscribes to the event while it's being fired. don't worry too much about this part
second, this copy (called handler in my code), is checked to see if there are any objects listening to the event (in which case the variable would be different than null)
then, the event is "fired", by calling handler and passing to it the "sender" of the event (in our case we put "this" meaning that it's the current object, which is logical), and the “event args” parameter which contains information about the event (in the case of a SelectionChanged for example, it can be the "Selected Item")

so, to recapitulate, an object defines an event (and gives it a corresponding event handler signature), interested objects listen to that event by declaring an event handler with the specified signature, which gets called when the event is fired
simple as that


  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

  2. And therefore to gmail account without phone number you have to verify your account with different phone numbers which are quite a problematic situation

  3. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.

    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

  4. This comment has been removed by the author.