旋转XAML形状后坐标发生变化

时间:2013-10-11 15:56:43

标签: c# xaml rotation windows-store-apps windows-8.1

我有移动和旋转的数字(网格)。它们放在父网格中(名称:字段)因此,为了移动我使用TranslateX,并进行轮换:

private void RightTap_Rotate(object sender, RightTappedRoutedEventArgs e)
    {
        var obj = sender as Grid;
        var objTransform = (CompositeTransform)obj.RenderTransform;
        Point cursorPos = e.GetPosition(obj);

        objTransform.CenterX = cursorPos.X;
        objTransform.CenterY = cursorPos.Y;

        objTransform.Rotation += 90;     
    }

即。我围绕它的中心旋转图。但旋转后我无法找到图形和场的正确相对位置。我用的是:

private void Object_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
    {
        var obj = sender as Grid;
        var objTransform = (CompositeTransform)obj.RenderTransform;

        var transform = Field.TransformToVisual(obj);
        Point relativePosition = transform.TransformPoint(new Point(0, 0));

        //then I use relativePosition.X/Y
    }

我想获得相对于字段左上角的形状左上角的坐标,但显然是在出现错误的旋转之后。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

在您的代码中,您正在将一个点从父对象转换为子对象。相反,您希望从子对象转换为父对象。

var obj = sender as Grid;

var transform = obj.TransformToVisual(Field);
Point parentPoint = transform.TransformPoint(new Point(0, 0));