Project Description
Implementation of custom routed events for silverlight 3, including a common compatablity layer so the same code base works for both silverlight and WPF.

This includes support for RegisterClassHandler/AddHandler implementations. Currently supports direct and bubble routing.

Enjoy !!
Andrew Whiddett and all the Development team (special thanks to Josh Wagoner/Jonathan Russ/Laurent Bugnion/Josh Smith)
And all from IdentityMine: http://www.identitymine.com

Creating custom Routed events really make life a lot easier - and is one of the superstars of the .net 3 platfom (we love you Microsoft) ..We thought that this functionality should be available to all Silverlight 3 developers :

Quick summary of implementation:-

Quick Quick Summary - Add 'Ex' to the end of the routed event classes - e.g. EventManagerEx/RoutedEventEx/RoutedEventArgsEx and the extension methods on UIElement e.g. RaiseEventEx/AddHandlerEx

Registering your custom routed event:-

(Attached event example shown)

_ public delegate void HoverEnterRoutedEventHandler(object sender, RoutedEventArgsEx e);
public static readonly RoutedEventEx HoverEnterEvent = EventManagerEx.RegisterRoutedEvent("HoverEnter", RoutingStrategy.Bubble, typeof(HoverEnterRoutedEventHandler), typeof(UserControl1));

public static void AddHoverEnterHandler(DependencyObject o, HoverEnterRoutedEventHandler handler)
{
((UIElement)o).AddHandlerEx(HoverEnterEvent, handler);
}

public static void RemoveHoverEnterHandler(DependencyObject o, HoverEnterRoutedEventHandler handler)
{
((UIElement)o).RemoveHandlerEx(HoverEnterEvent, handler);
}
_

Raising an event:

_ private void OnClick(object sender, System.Windows.RoutedEventArgs e)
{
this.RaiseEventEx(new RoutedEventArgsEx(HoverEnterEvent));
}
_

Subscription to an event:

(Class handler style):

_public MainPage()
{
// Required to initialize variables
InitializeComponent();

EventManagerEx.RegisterClassHandler(typeof(MainPage), UserControl1.HoverEnterEvent, new UserControl1.HoverEnterRoutedEventHandler(OnHover));
}
_

(Add Handler style):

_private void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
{
UserControl1.AddHoverEnterHandler(this, new UserControl1.HoverEnterRoutedEventHandler(OnHover));
}
_

Last edited May 15, 2009 at 9:38 PM by andrewwhiddett, version 6