Multi-Touch UI:同时拖放多个对象

时间:2012-12-31 15:56:51

标签: windows-phone-7 drag-and-drop expression-blend windows-phone

有没有办法在Windows Phone中同时移动多个对象? (如果我移动左右索引,我想移动两个项目)

我找到了两种在Windows Phone上实现拖放功能的解决方案。但是,我只能同时移动一个项目。

使用Expression Blend

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
       <Image Margin="129,112,167,122" Source="mypicture.png" Stretch="Fill">
                <Custom:Interaction.Behaviors>
                    <il:MouseDragElementBehavior/>
                </Custom:Interaction.Behaviors>
       </Image>
</Grid>

使用Windows Phone Toolkit

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
             <Rectangle x:Name="myRectangle" Width="200" Height="200" Fill="Blue">
                 <toolkit:GestureService.GestureListener>
                     <toolkit:GestureListener DragDelta="GestureListener_DragDelta"/> 
                 </toolkit:GestureService.GestureListener>
             </Rectangle>
 </Grid>

1 个答案:

答案 0 :(得分:2)

事实上,解决方案是使用Touch.FrameReported

// MainPage.xaml
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Rectangle x:Name="RedRect" Width="100" Height="100" Fill="Red">
        <Rectangle.RenderTransform>
            <TranslateTransform x:Name="RedTransform" Y="-100" />
        </Rectangle.RenderTransform>
    </Rectangle>
    <Rectangle x:Name="BlueRect" Width="100" Height="100" Fill="Blue">
        <Rectangle.RenderTransform>
            <TranslateTransform x:Name="BlueTransform" Y="100" />
        </Rectangle.RenderTransform>
    </Rectangle>
</Grid>

// MainPage.xaml.cs
public partial class MainPage : PhoneApplicationPage
{
    private Dictionary<int, RectInfo> _rects = new Dictionary<int, RectInfo>();

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Register handler for Touch.FrameReported events
        Touch.FrameReported += new TouchFrameEventHandler(OnFrameReported);
    }

    private void OnFrameReported(object sender, TouchFrameEventArgs e)
    {
        TouchPointCollection points = e.GetTouchPoints(null);

        foreach (TouchPoint point in points)
        {
            if (point.Action == TouchAction.Down)
            {
                // Find out if a rectangle was touched
                Rectangle rect = null;

                if (point.TouchDevice.DirectlyOver == RedRect)
                    rect = RedRect;
                else if (point.TouchDevice.DirectlyOver == BlueRect)
                    rect = BlueRect;

                // If the answer is yes, associate the "device" (finger) ID with
                // the rectangle and store information regarding that rectangle.
                // Then change the rectangle's fill color to yellow.
                if (rect != null)
                {
                    TranslateTransform transform =
                        rect.RenderTransform as TranslateTransform;
                    RectInfo ri = new RectInfo() { Rect = rect, Translation =
                        new Point(transform.X, transform.Y), StartPos = point.Position,
                        Fill = rect.Fill };
                    _rects.Add(point.TouchDevice.Id, ri);
                    rect.Fill = new SolidColorBrush(Colors.Yellow);
                }
            }
            else if (point.Action == TouchAction.Move)
            {
                // Find the rectangle (if any) associated with the finger being moved
                int id = point.TouchDevice.Id;

                RectInfo ri = null;
                _rects.TryGetValue(id, out ri);

                if (ri != null)
                {
                    Rectangle rect = ri.Rect;
                    TranslateTransform transform =
                        rect.RenderTransform as TranslateTransform;

                    // Get the current position of the cursor
                    Point pos = point.Position;

                    // Compute the offset from the starting position
                    double dx = pos.X - ri.StartPos.X;
                    double dy = pos.Y - ri.StartPos.Y;

                    // Apply the deltas to the transform
                    transform.X = ri.Translation.X + dx;
                    transform.Y = ri.Translation.Y + dy;
                }
            }
            else if (point.Action == TouchAction.Up)
            {
                // Find the rectangle (if any) associated with the finger being moved
                int id = point.TouchDevice.Id;

                RectInfo ri = null;
                _rects.TryGetValue(id, out ri);

                if (ri != null)
                {
                    // Restore the original fill color of the rectangle associated
                    // with the finger that was just lifted
                    Rectangle rect = ri.Rect;
                    rect.Fill = ri.Fill;

                    // Remove the finger ID from the dictionary
                    _rects.Remove(id);
                }
            }
        }
    }
}

public class RectInfo
{
    public Rectangle Rect;
    public Point Translation;
    public Point StartPos;
    public Brush Fill;
}