Windows phone 7 多点触摸UI:同时拖放多个对象

Windows phone 7 多点触摸UI:同时拖放多个对象,windows-phone-7,drag-and-drop,expression-blend,windows-phone,Windows Phone 7,Drag And Drop,Expression Blend,Windows Phone,在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=

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

事实上,解决方案是使用
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;
}
//MainPage.xaml
//MainPage.xaml.cs
公共部分类主页:PhoneApplicationPage
{
专用词典_rects=新词典();
//建造师
公共主页()
{
初始化组件();
//为Touch.FrameReported事件注册处理程序
Touch.FrameReported+=新的TouchFrameEventHandler(OnFrameReported);
}
私有void OnFrameReported(对象发送方,TouchFrameEventArgs e)
{
TouchPointCollection points=e.GetTouchPoints(空);
foreach(以点为单位的接触点)
{
如果(point.Action==TouchAction.Down)
{
//找出是否触摸了矩形
矩形rect=null;
if(point.TouchDevice.DirectlyOver==RedRect)
rect=RedRect;
else if(point.TouchDevice.DirectlyOver==BlueRect)
rect=BlueRect;
//如果回答是“是”,请将“设备”(手指)ID与
//矩形并存储有关该矩形的信息。
//然后将矩形的填充颜色更改为黄色。
if(rect!=null)
{
平移变换=
将rect.RenderTransform转换为TranslateTransform;
RectInfo ri=new RectInfo(){Rect=Rect,Translation=
新点(transform.X,transform.Y),起始点=点位置,
Fill=rect.Fill};
_rects.Add(point.TouchDevice.Id,ri);
rect.Fill=新的SolidColorBrush(Colors.Yellow);
}
}
else if(point.Action==TouchAction.Move)
{
//查找与正在移动的手指关联的矩形(如果有)
int id=point.TouchDevice.id;
rectinfori=null;
_TryGetValue(id,out ri);
如果(ri!=null)
{
矩形rect=ri.rect;
平移变换=
将rect.RenderTransform转换为TranslateTransform;
//获取光标的当前位置
点位置=点位置;
//计算从起始位置的偏移量
双dx=位置X-ri.StartPos.X;
双dy=位置Y-起始位置Y;
//将增量应用于变换
transform.X=ri.Translation.X+dx;
transform.Y=ri.Translation.Y+dy;
}
}
else if(point.Action==TouchAction.Up)
{
//查找与正在移动的手指关联的矩形(如果有)
int id=point.TouchDevice.id;
rectinfori=null;
_TryGetValue(id,out ri);
如果(ri!=null)
{
//恢复关联矩形的原始填充颜色
//用刚刚举起的手指
矩形rect=ri.rect;
rect.Fill=ri.Fill;
//从字典中删除手指ID
_rects.Remove(id);
}
}
}
}
}
公共类RectInfo
{
公共矩形矩形;
公共点翻译;
公共点StartPos;
公共灌木丛;
}

您是如何获得名称(
x:name
)元素的
单击的
(或触摸的)?这让我抓狂,我能用TouchDevce做的就是直接获取它的元素类型。哪个事件?触摸,报告了吗?单击?用于单击。因为当我使用,
point.TouchDevice.DirectlyOver
时,它只给出元素类型。