gpt4 book ai didi

wpf - WPF自定义路由事件-如何退订?

转载 作者:行者123 更新时间:2023-12-04 13:20:43 25 4
gpt4 key购买 nike

如何从一个自定义路由事件中注销。

我有以下代码(对于自定义路由事件非常标准)

//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
});
}

现在,一个类(class)将在此行中预订此事件:
//Receive the Movement events
EventManager.RegisterClassHandler(
typeof(Window)
, Main.MovementEvent
, new Main.MovementRoutedEventHandler(MovementHandler));

当我关闭类实例时,我应该从事件中取消订阅(否则,实例将不会被垃圾回收)。

我该怎么称呼?我尝试了 RegisterClassHandler(typeof(Window), Main.MovementEvent, **null**),但出现了异常...

任何帮助欢迎。提前致谢。

JM

最佳答案

您可以从System.Windows.Window类(通常从UIElement类)使用RemoveHandler方法。

代码看起来像这样:

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

关于wpf - WPF自定义路由事件-如何退订?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7866458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com