0

我在我的 xaml 中有一个如下定义的列表框。每个项目都包含一个画布,其中包含一个图像元素。我已经声明ManipulationEvents了图像。

<ListBox x:Name="CategoryLB" SelectionChanged="CategoryClicked" Margin="0,131,0,0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Canvas Width='460'  Height="130" Background="#FF0D6B97" Margin="10,10,10,10" >
                   <Image Width='480'  Height="150" Source="{Binding Page}" Stretch="None" Opacity="1" CacheMode="BitmapCache"
                         ManipulationDelta="ImageManipulationDelta"
                         ManipulationCompleted="ImageManipulationCompleted"
                         ManipulationStarted="ImageManipulationStarted"/>        
            </Canvas>
                    </DataTemplate>
        </ListBox.ItemTemplate>
</ListBox>

问题是ManipulationDelta根本没有被解雇。只有ManipulationStartedandManipulationCompleted被调用,因此我的动画没有得到翻译值。对我来说看起来很简单。

这些是 ManipulationEvents

    private void ImageManipulationStarted(object sender, ManipulationStartedEventArgs e)
    {
        FrameworkElement transformElement = ((FrameworkElement)sender) as FrameworkElement;
        transformElement.SetHorizontalOffset(0);
    }

    private void ImageManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
    {
        FrameworkElement transformElement = ((FrameworkElement)sender) as FrameworkElement;

        if (Math.Abs(e.TotalManipulation.Translation.X) > transformElement.ActualWidth / 3)
        {
            if (e.TotalManipulation.Translation.X < 0.0)
            {
                ToDoItemDeletedAction(transformElement);
            }
            else
            {
                ToDoItemCompletedAction(transformElement);
            }
        }
        else
        {
            ToDoItemBounceBack(transformElement);
        }
    }

    private void ImageManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        FrameworkElement transformElement = ((FrameworkElement)sender) as FrameworkElement;

        // handle the drag to offset the element
        double offset = transformElement.GetHorizontalOffset().Value + e.DeltaManipulation.Translation.X;
        transformElement.SetHorizontalOffset(offset);
    }    

我在这里缺少什么吗?

4

2 回答 2

1

你的代码没问题,但问题是 delta 事件不会在模拟器上触发,我已经在模拟器和设备上测试了你的代码,并且它在设备上工作正常,所以不要担心并在设备上测试你的代码....

于 2013-07-12T12:39:47.287 回答
0

我已经对图像进行了处理,类似于这样..我希望这可以帮助你..

<Image x:Name="imgViewer" CacheMode="BitmapCache" Source="/MetroImages/mapmetro.png" Stretch="Uniform">
                            <Image.RenderTransform>
                                <ScaleTransform x:Name="scaleTrans" ScaleX="2" ScaleY="2" CenterX="150" CenterY="200"  />
                            </Image.RenderTransform>
                            <toolkit:GestureService.GestureListener>
                                <toolkit:GestureListener 
            PinchStarted="GestureListener_PinchStart" 
            PinchDelta="GestureListener_PinchDelta" 
            PinchCompleted="GestureListener_PinchComplete"
                    DragStarted="GestureListener_DragStart"
            DragDelta="GestureListener_DragDelta"
            DragCompleted="GestureListener_DragCompleted"/>                                  
                            </toolkit:GestureService.GestureListener>
                        </Image>

然后在事件处理程序中

private readonly DispatcherTimer m_animationTimer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(10) };
        private double _cx, _cy;

        private void GestureListener_PinchStart(object sender, PinchStartedGestureEventArgs e)
        {
            Point p1 = e.GetPosition(imgViewer, 0);
            Point p2 = e.GetPosition(imgViewer, 1);

            scaleTrans.CenterX = (p1.X + ((p2.X - p1.X) / 2));
            scaleTrans.CenterY = (p1.Y + ((p2.Y - p1.Y) / 2));

            _cx = scaleTrans.ScaleX;
            _cy = scaleTrans.ScaleY;
        }

        private void GestureListener_PinchDelta(object sender, PinchGestureEventArgs e)
        {
            // Compute new scaling factors
            double cx = _cx * e.DistanceRatio;
            double cy = _cy * e.DistanceRatio;

            // If they're between 1.0 and 4.0, inclusive, apply them
            if (cx >= 1.0 && cx <= 4.0 && cy >= 1.0 && cy <= 4.0)
            {
                if ((cy - 1) < 0.1 && (cx - 1) < 0.1)
                    cx = cy = 1;

                scaleTrans.ScaleX = cx;
                scaleTrans.ScaleY = cy;
            }
        }

        private void GestureListener_PinchComplete(object sender, PinchGestureEventArgs e)
        {
        }

        private void GestureListener_DragStart(object sender, DragStartedGestureEventArgs e)
        {
        }

        private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            scaleTrans.CenterX = (scaleTrans.CenterX - e.HorizontalChange);
            scaleTrans.CenterY = (scaleTrans.CenterY - e.VerticalChange);

            if (scaleTrans.CenterX < 0)
            {
                scaleTrans.CenterX = 0;
            }
            else if (scaleTrans.CenterX > imgViewer.ActualWidth)
                scaleTrans.CenterX = imgViewer.ActualWidth;

            if (scaleTrans.CenterY < 0)
            {
                scaleTrans.CenterY = 0;
            }
            else if (scaleTrans.CenterY > imgViewer.ActualHeight)
                scaleTrans.CenterY = imgViewer.ActualHeight;
        }

        private void GestureListener_DragCompleted(object sender, DragCompletedGestureEventArgs e)
        {
        }
于 2013-07-12T09:45:24.060 回答