Search code examples
wpfeventsrouted

WPF Custom Routed Events - How to UnSubscribe?


How to DeRegister from one Custom Routed Event.

I have the following code (very standard for Custom Routed Events)

//Dispatch the Video Detection Movements
public delegate void MovementRoutedEventHandler( object sender
                                                , MovementRoutedEventArgs e);
public class MovementRoutedEventArgs : RoutedEventArgs
{
    private readonly DahuaDevice _device;
    private readonly byte[] _canals;
    private readonly DateTime _when;

    public MovementRoutedEventArgs(DahuaDevice device, byte[] canals, DateTime when)
    {
        _device = device;
        _canals = canals;
        _when = when;
    }
    public DahuaDevice Device
    {
        get { return _device; }
    }
    public Byte[] Canals
    {
        get { return _canals; }
    }
    public DateTime When
    {
        get { return _when; }
    }
}
public static RoutedEvent MovementEvent = EventManager.RegisterRoutedEvent(
        "Movement"
    , RoutingStrategy.Tunnel
    , typeof(MovementRoutedEventHandler)
    , typeof(Window)
);

public event RoutedEventHandler Movement
{
    add { AddHandler(MovementEvent, value); }
    remove { RemoveHandler(MovementEvent, value); }
}
public void RaiseMovementEvent(DahuaDevice device, byte[] canals, DateTime when)
{
    RaiseEvent(new MovementRoutedEventArgs(device, canals, when)
    {
        RoutedEvent = MovementEvent
    });
}

Now a class will subscribe to this event with this line:

//Receive the Movement events
EventManager.RegisterClassHandler( 
      typeof(Window)
    , Main.MovementEvent
    , new Main.MovementRoutedEventHandler(MovementHandler));

When I close the class instance, I should UnSubscribe from the event (otherwise, instance won't be garbage collected).

What should I call? I tried RegisterClassHandler(typeof(Window), Main.MovementEvent, **null**) but I get an exception...

Any help welcome. Thanks in advance.

JM


Solution

  • You can use RemoveHandler method from System.Windows.Window class (generally from UIElement class).

    The code could look something like this:

    Main.RemoveHandler( Main.MovementEvent
                      , new Main.MovementRoutedEventHandler(MovementHandler));