删除EventHandler不会显示任何效果

时间:2011-12-09 13:32:54

标签: c# wpf event-handling video-streaming kinect

我想从Kinect暂停一个VideoStream。该按钮具有以下代码:

if (capturing==0)
{
    try
    {
        // Signing the Event for Image frame ready
        nuiRuntime.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_VideoFrameReady);
        nuiRuntime.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_DepthFrameReady);
        nuiRuntime.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
        nuiRuntime.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.Depth);
    }
    catch
    {
        Dialogs.ShowWarning("An Error occured. Check, if the Kinect is connected properly.");
    }
    finally
    {
        capturing = 2;
    }
}
else if (capturing == 1)
{
    nuiRuntime.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_VideoFrameReady);
    nuiRuntime.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_DepthFrameReady);
    capturing = 2;//go on after break 
}
else
{
    nuiRuntime.VideoFrameReady -= new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_VideoFrameReady);
    nuiRuntime.DepthFrameReady -= nuiRuntime_DepthFrameReady;
    capturing = 1;//pause
}

我不认为,这很难理解。 状态:0,Kinect未初始化,这样做         1,Kinect暂停,继续         2,Kinect正常捕捉

但是:当执行“else”时,GUI中的图像(WPF.Image)仍然显示移动的视频流。在“其他”是两种方式,我试图让它休息一下。然后应该显示一张静止图片。

当然,我只能说EventHandler:如果我们处于状态1,根本就不在图片上工作,但我不认为,这是一种干净的实现方式。

任何人都可以看到我的错误吗?

1 个答案:

答案 0 :(得分:-3)

事件处理程序是引用类型(委托),即您添加到事件的引用类型

nuiRuntime.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_VideoFrameReady);

与您删除的不一样:

nuiRuntime.VideoFrameReady -= new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_VideoFrameReady);

修复很简单,将处理程序实例保留在某处:

var handler = new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_VideoFrameReady);

然后添加/删除相同的实例:

nuiRuntime.VideoFrameReady += handler;
nuiRuntime.VideoFrameReady -= handler;

BTW,您在&#34;其他&#34;中使用的代码块:

nuiRuntime.DepthFrameReady -= nuiRuntime_DepthFrameReady;

与:

相同
nuiRuntime.DepthFrameReady -= new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_DepthFrameReady);

编译器简单为你做new EventHandler<>事。