如何在canvas Control中获取图像的x和y坐标

时间:2013-12-16 20:42:35

标签: windows-phone-8

您好我正在开窗口Phone 8应用程序我希望在图像拖放时将图像的x,y坐标设置为画布控件并将图像设置为特定的x,y坐标

2 个答案:

答案 0 :(得分:0)

您可以使用GeneralTransform获取位置。例如,你有xaml:

<Canvas Name="mycanvas" Grid.Row="3">
     <Image Name="myImage" Source="/Resources/Image1.png" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="65"/>
</Canvas>

然后在代码中你可以使用:

private Point Get_Position()
{
    GeneralTransform generalTr = mycanvas.TransformToVisual(myImage);
    Point current = generalTr.Transform(new Point(0, 0));
    return current;
}

您可以在MSDN

了解更多信息

答案 1 :(得分:0)

对于拖放我建议您使用Windows Phone Toolkit GestureListener: 这里有更多描述:http://www.geekchamp.com/articles/wp7-gestureservice-in-depth--key-concepts-and-api 下面是一些例子:

的Xaml:

<Canvas x:Name="ImageCanvas" Margin="0,100,0,0" Background="Gray"  Canvas.ZIndex="-1" SizeChanged="Root_OnSizeChanged">
            <Canvas.Clip>
                <RectangleGeometry x:Name="RectGeometry"/>
            </Canvas.Clip>
            <Image Name="image"
               Source="Your Source" 
               Stretch="None"
                HorizontalAlignment="Center"
               VerticalAlignment="Center" Canvas.ZIndex="-1">
                <Image.RenderTransform>
                    <TransformGroup >
                        <MatrixTransform x:Name="previousTransform" />
                        <TransformGroup x:Name="currentTransform">
                            <ScaleTransform x:Name="scaleTransform" />
                            <RotateTransform x:Name="rotateTransform" />
                            <TranslateTransform x:Name="translateTransform"/>
                        </TransformGroup>
                    </TransformGroup>
                </Image.RenderTransform>
                <toolkit:GestureService.GestureListener>
                    <toolkit:GestureListener DragStarted="OnDragStarted"
                                     DragDelta="OnDragDelta"
                                     DragCompleted="OnDragCompleted"
                                     PinchStarted="OnPinchStarted"
                                     PinchDelta="OnPinchDelta"
                                     PinchCompleted="OnPinchCompleted" GestureCompleted="GestureListener_OnGestureCompleted"  />
                </toolkit:GestureService.GestureListener>    
            </Image>
            </Canvas>

代码背后:

私人布尔_isDragging;         私人布尔_isPinching;

 private void OnDragStarted(object sender, DragStartedGestureEventArgs args)
        {
            _isDragging = args.OriginalSource == image;
            _xChange = _yChange = 0;
        }

        private void OnDragDelta(object sender, DragDeltaGestureEventArgs args)
        {

            if (_isDragging)
            {                 

                translateTransform.X += args.HorizontalChange;
                translateTransform.Y += args.VerticalChange;



            }
        }

        private void OnDragCompleted(object sender, DragCompletedGestureEventArgs args)
        {

            if (_isDragging)
            {

                _isDragging = false;

            }
            _dragCompleted = true;


        }