更改DependencyObject类型以修复“必须是Visual3D类型”。错误?

时间:2015-02-24 11:14:13

标签: c# xaml 3d kinect dependencyobject

我有一个问题,我需要这样做(当我点击一个Viewport3D时,一个代码知道X,Y,Z坐标,它显示了使用Kinect获得的3D图像):

        bool success;
        Viewport3DVisual viewport3DVisual = VisualTreeHelper.GetParent(GraphicsViewport) as Viewport3DVisual;
        DependencyObject visual = new DependencyObject();
        Matrix3D screenTransform = MathUtils.TryTransformTo2DAncestor(visual, out viewport3DVisual, out success);

        if (success)
        {
            Point3D scenePoint = new Point3D(mouseLocation.X, mouseLocation.Y, 0);
            Point3D screenPoint = screenTransform.Transform(scenePoint);
            MessageBox.Show("screenPoint: " + screenPoint.X + " " + screenPoint.Y + " " + screenPoint.Z);
        }

        if (screenTransform.HasInverse)
        {
            Matrix3D reverseTransform = screenTransform;
            reverseTransform.Invert();
            Point3D pointOnScreen = new Point3D(mouseLocation.X, mouseLocation.Y, 1); // you need to choose the z-depth
            Point3D pointInWorld = reverseTransform.Transform(pointOnScreen);
            MessageBox.Show("pointInWorld: " + pointInWorld.X + " " + pointInWorld.Y + " " + pointInWorld.Z);
        }

但是当我运行代码时,它在MathUtils.cs上给出了我的异常:

if (!(visual is Visual3D))
        {
            throw new ArgumentException("Must be of type Visual3D.", "visual");
        }

我试图将DependencyObject类型更改为Visual3D,但我不知道该怎么做。

任何人都可以帮我修复错误并使代码正常工作吗?

非常感谢!

0 个答案:

没有答案