1

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

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

使用表情混合

<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 工具包

<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>
4

1 回答 1

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;
}
于 2013-01-02T17:46:36.950 回答