1

我在 winForms 中有一个可用的用户控件,并决定将其移植到 WPF。该控件是 kinect 传感器的预览控件,支持所有 3 个流(颜色、深度、骨架)。当我更改 ViewMode(我的控件的一个属性)时,它应该从事件中注销处理程序。

看一下代码:

switch (viewMode)
{
    case KinectViewMode.Color:
        Kinect.ColorFrameReady -= new EventHandler<ColorImageFrameReadyEventArgs>(kinect_ColorFrameReady);
        if (!DoesEventHeveListeners("_colorFrameReadyContextHandler"))
            Kinect.ColorStream.Disable();
        break;
    case KinectViewMode.Depth:
        Kinect.DepthFrameReady -= kinect_DepthFrameReady;
        if (!DoesEventHeveListeners("_depthFrameReadyContextHandler"))
            Kinect.DepthStream.Disable();
        break;
    case KinectViewMode.Skeleton:
        Kinect.SkeletonFrameReady -= kinect_SkeletonFrameReady;
        if (!DoesEventHeveListeners("_skeletonFrameReadyContextHandler"))
        {
            Kinect.SkeletonStream.Disable();
        }
        break;
    case KinectViewMode.None:
        Kinect.SkeletonFrameReady -= kinect_SkeletonFrameReady;
        if (!DoesEventHeveListeners("_skeletonFrameReadyContextHandler"))
        {
            Kinect.SkeletonStream.Disable();
        }
        Kinect.DepthFrameReady -= kinect_DepthFrameReady;
        if (!DoesEventHeveListeners("_depthFrameReadyContextHandler"))
            Kinect.DepthStream.Disable();
        Kinect.ColorFrameReady -= kinect_ColorFrameReady;
        if (!DoesEventHeveListeners("_colorFrameReadyContextHandler"))
            Kinect.ColorStream.Disable();
        registeredToEvent = false;
        break;
    }
}

viewMode = value;

switch (viewMode)
{
    case KinectViewMode.Color:
        Kinect.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinect_ColorFrameReady);
        if (!Kinect.ColorStream.IsEnabled)
            Kinect.ColorStream.Enable();
        break;
    case KinectViewMode.Depth:
        Kinect.DepthFrameReady += new EventHandler<DepthImageFrameReadyEventArgs>(kinect_DepthFrameReady);
        if (!Kinect.DepthStream.IsEnabled)
        {
            depthPixels = new short[Kinect.DepthStream.FramePixelDataLength];
            colorPixels = new byte[sizeof(int) * Kinect.DepthStream.FramePixelDataLength];
            Kinect.DepthStream.Enable();
        }
        break;
    case KinectViewMode.Skeleton:
        Kinect.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(kinect_SkeletonFrameReady);
        if (!Kinect.SkeletonStream.IsEnabled)
            Kinect.SkeletonStream.Enable();
        break;
    case KinectViewMode.None:
        break;
}

if (ViewMode != KinectViewMode.None)
    registeredToEvent = true;

为什么此代码在 winForms 中有效,而在 WPF 中无效?

4

0 回答 0