none
如何拖动一个 Rectangle ? RRS feed

答案

  • 以下是三个实现拖拽效果的关键点:

    1. 监控Tap和Hold– MouseDown Event.
    2. 拖动物体至坐标点– MouseMove Event.
    3. 释放物体在坐标点上- MouseUp Event.

    主要相关代码:

    private void MouseMoving(object sender, MouseEventArgs e)
            {
                if (sender.GetType() == typeof(Rectangle))
                {
                    Rectangle cx = (Rectangle)sender;
                    Canvas.SetZIndex(cx, idx++);
                }
                if (sender.GetType() == typeof(Ellipse))
                {
                    Ellipse cx = (Ellipse)sender;
                    Canvas.SetZIndex(cx, idx++);
                }
                if (sender.GetType() == typeof(Path))
                {
                    Path cx = (Path)sender;
                    Canvas.SetZIndex(cx, idx++);
                }
                else if (sender.GetType() == typeof(TextBlock))
                {
                    TextBlock realSender = (TextBlock)sender;
                    Canvas.SetZIndex(realSender, idx++);
                }
                else if (sender.GetType() == typeof(Image))
                {
                    Image realSender = (Image)sender;
                    Canvas.SetZIndex(realSender, idx++);
                }
            }

    具体参考:

    http://developer.nokia.com/community/wiki/Windows_Phone%E7%9A%84%E6%8B%96%E6%8B%BD

    2014年10月20日 2:49