Routed events in Silverlight 4.0
Hi friends in this article I will tell you What is routed events in Silverlight in shortly
1. What is routed Events
¡ Routed events are events with more traveling power—they can tunnel down or bubble up the element tree and be processed by event handlers along the way.
¡ Event routing allows an event to originate in one element but be raised by another one.
2. Defining, Registering, and Wrapping a Routed Event
3. Wrapping a Routed Event
4. Registering
¡ When registering an event, you need to specify the name of the event, the type of routine, the delegate that defines the syntax of the event handler, and the class that owns the event.
¡ Usually, routed events are wrapped by ordinary .NET events to make them accessible to all .NET
Languages. The event wrapper adds and removes registered callers using the Add Handler() and
Remove Handler () methods, both of which are defined in the base Framework Element class and Inherited by every WPF element.
5. Raising a Routed Event
¡ The defining class needs to raise it at some point.
¡ Exactly where this takes place is an implementation detail.
¡ However, the important detail is that your event is not raised through the traditional .NET event wrapper. Instead, you use the RaiseEvent() method that every element inherits from the UIElement class.
¡ RoutedEventArgs e = new RoutedEventArgs(ButtonBase.ClickEvent, this);
base.RaiseEvent(e);
¡ The RaiseEvent() method takes care of firing the event to every caller that’s been registered with the AddHandler() method.
¡ All WPF events use the familiar .NET convention for event signatures.
¡ That first parameter of every event handler provides a reference to the object that fired the event (the sender).
¡ The second parameter is an EventArgs object that bundles together any additional details that might be important.
¡ private void img_MouseUp(object sender, MouseButtonEventArgs e)
6. Handling a Routed Event
¡ The most common approach is to add an event attribute to your XAML markup.
<Image Source="happyface.jpg" Stretch="None“ Name="img" MouseUp="img_MouseUp" />
¡ img.MouseUp += new MouseButtonEventHandler(img_MouseUp);
¡ img.MouseUp += img_MouseUp;
¡ img.AddHandler(Image.MouseUpEvent, new MouseButtonEventHandler(img_MouseUp));
The code approach is useful if you need to dynamically create a control and attach an event handler at some point during the lifetime of your window. By comparison, the events you hook up in XAML are always attached when the window object is first instantiated. The code approach also allows you to keep your XAML simpler and more streamlined, which is perfect if you plan to share it with nonprogrammers, such as a design artist. The drawback is a significant amount of boilerplate code that will clutter up your code files.
Regards
Sujeet Bhujbal
Here, I found Asp.Net code which help to manage events in several pages as well as good idea to share knowledge.
ReplyDelete