"The best overloaded method match for ... has some invalid arguments",开发 Kinect v2

"The best overloaded method match for ... has some invalid arguments", work on the Kinect v2

我正在使用 Kinect v2 进行右手的手势检测,并得到了一个我可以使用的好样本,但在错误时遇到了代码转换问题:"The best overloaded method match for ... has some invalid arguments" 出现在几个但相似的代码行中。我已经检查过此错误的早期问题,但找不到针对此特定问题的解决方案。非常感谢您的帮助!

// Gesture detection

    public enum HandPosition { Unknown, Up, Center, Down, Left, Right, Backwards, Forwards }

    public class HandPositionChangedArgs
    {
        public HandPosition Position { get; set; }
    }

    public delegate void HandPositionChangedDelegate(object sender, HandPositionChangedArgs args);

    public static class GestureDetection
    {
        public static void FrameReady(Microsoft.Kinect.Body skeleton)
        {
            ProcessRightHand(skeleton);
        }

        // Right Hand
        private static Dictionary<int, HandPosition> rightHandUpDownDictionary = new Dictionary<int, HandPosition>();
        private static Dictionary<int, HandPosition> rightHandLeftRightDictionary = new Dictionary<int, HandPosition>();
        private static Dictionary<int, HandPosition> rightHandBackForwardsDictionary = new Dictionary<int, HandPosition>();

        public static event HandPositionChangedDelegate RightHandUpDownChanged;
        public static event HandPositionChangedDelegate RightHandLeftRightChanged;
        public static event HandPositionChangedDelegate RightHandBackForwardsChanged;

        public static void ProcessRightHand(Microsoft.Kinect.Body skeleton)
        {
            Joint rightHand = skeleton.Joints[Microsoft.Kinect.JointType.HandRight];
            Joint rightShoulder = skeleton.Joints[Microsoft.Kinect.JointType.ShoulderRight];

            CameraSpacePoint rightHandPoint = rightHand.Position;
            CameraSpacePoint rightShoulderPoint = rightShoulder.Position;

            HandPosition previousRightHandUpDownPosition = (rightHandUpDownDictionary.ContainsKey(skeleton.TrackingId)) ? rightHandUpDownDictionary[skeleton.TrackingId] : HandPosition.Unknown; // Here's the error
            HandPosition newRightHandUpDownPosition = HandPosition.Unknown;

            HandPosition previousRightHandLeftRightPosition = (rightHandLeftRightDictionary.ContainsKey(skeleton.TrackingId)) ? rightHandLeftRightDictionary[skeleton.TrackingId] : HandPosition.Unknown; // Here's the error
            HandPosition newRightHandLeftRightPosition = HandPosition.Unknown;

            HandPosition previousRightHandBackForwardsPosition = (rightHandBackForwardsDictionary.ContainsKey(skeleton.TrackingId)) ? rightHandBackForwardsDictionary[skeleton.TrackingId] : HandPosition.Unknown;// Here's the error
            HandPosition newRightHandBackForwardsPosition = HandPosition.Unknown;

            if ((rightHand.TrackingState == TrackingState.NotTracked) || (rightShoulder.TrackingState == TrackingState.NotTracked)) // From JointTrackingState to TrackingState?
            {
                newRightHandUpDownPosition = HandPosition.Unknown;
                newRightHandLeftRightPosition = HandPosition.Unknown;
                newRightHandBackForwardsPosition = HandPosition.Unknown;
            }
            else
            {
                // Up/Down
                if (rightHandPoint.Y - rightShoulderPoint.Y > 0.2)
                {
                    newRightHandUpDownPosition = HandPosition.Up;
                }
                else if (Math.Abs(rightHandPoint.Y - rightShoulderPoint.Y) > 0.2)
                {
                    newRightHandUpDownPosition = HandPosition.Down;
                }
                else
                {
                    newRightHandUpDownPosition = HandPosition.Center;
                }

                // Left/Right
                if (rightHandPoint.X - rightShoulderPoint.X > 0.2)
                {
                    newRightHandLeftRightPosition = HandPosition.Right;
                }
                else if (Math.Abs(rightHandPoint.X - rightShoulderPoint.X) > 0.2)
                {
                    newRightHandLeftRightPosition = HandPosition.Left;
                }
                else
                {
                    newRightHandLeftRightPosition = HandPosition.Center;
                }

                // Backwards/Forwards
                if (rightShoulderPoint.Z - rightHandPoint.Z > 0.5)
                {
                    newRightHandBackForwardsPosition = HandPosition.Forwards;
                }
                else if (rightShoulderPoint.Z - rightHandPoint.Z < 0.25)
                {
                    newRightHandBackForwardsPosition = HandPosition.Backwards;
                }
                else
                {
                    newRightHandBackForwardsPosition = HandPosition.Center;
                }
            }

            if (previousRightHandUpDownPosition != newRightHandUpDownPosition)
            {
                rightHandUpDownDictionary[skeleton.TrackingId] = newRightHandUpDownPosition; // Here's the error
                if (RightHandUpDownChanged != null)
                {
                    RightHandUpDownChanged(skeleton, new HandPositionChangedArgs() { Position = newRightHandUpDownPosition });
                }
            }

            if (previousRightHandLeftRightPosition != newRightHandLeftRightPosition)
            {
                rightHandLeftRightDictionary[skeleton.TrackingId] = newRightHandLeftRightPosition; // Here's the error
                if (RightHandLeftRightChanged != null)
                {
                    RightHandLeftRightChanged(skeleton, new HandPositionChangedArgs() { Position = newRightHandLeftRightPosition });
                }
            }

            if (previousRightHandBackForwardsPosition != newRightHandBackForwardsPosition)
            {
                rightHandBackForwardsDictionary[skeleton.TrackingId] = newRightHandBackForwardsPosition; // Here's the error
                if (RightHandBackForwardsChanged != null)
                {
                    RightHandBackForwardsChanged(skeleton, new HandPositionChangedArgs() { Position = newRightHandBackForwardsPosition });
                }
            }
        }
    }

您将各种词典定义为 Dictionary<int, HandPosition>,但在代码中您使用 skeleton.TrackingId 来引用词典的键。
根据MSDNBody.TrackingId是一个ulong

您需要将词典重新定义为 Dictionary<ulong, HandPosition> 或将 skeleton.TrackingId 转换为 Int 变量才能使用它。