0

我正在开发一个 Windows Phone 应用程序,我创建了改变图像位置的故事板。但我有一个错误。

System.Windows.ni.dll 中发生“System.InvalidOperationException”类型的异常,但未在用户代码故事板中处理

这是我的代码:

XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" MouseMove="Mouse_Move">
    <Grid.Resources>
        <Storyboard x:Name="sbimg">
            <PointAnimation x:Name="animationimg" Duration="0:0:0.1"
                    Storyboard.TargetName="earimg" />
        </Storyboard>
    </Grid.Resources>                    
    <Image x:Name="earimg" Height="30" Width="30"
        Source="1.png" Margin="0,0,426,577">
    </Image>             
</Grid>

和 C#:

private void Mouse_Move(object sender, System.Windows.Input.MouseEventArgs e)
{
    double pointX = e.GetPosition(null).X;
    double pointY = e.GetPosition(null).Y;            
    Point mypoint = new Point(pointX,pointY);
    animationimg.To = mypoint;
    sbimg.Begin();
}
4

1 回答 1

0

为了完成这项任务,我建议使用CanvasasLayoutRootGestureListnerfromWindows Phone Toolkit来捕捉用户手势。你XAML应该看起来像这样

 <Canvas x:Name="LayoutRoot" 
            Background="Transparent">
        <toolkit:GestureService.GestureListener>
            <toolkit:GestureListener
                    x:Name="SurfaceGestureListner"
                     Tap="SurfaceGestureListner_OnTap"
                    DragDelta="GestureListnerDragDelta"/>
        </toolkit:GestureService.GestureListener>
        <Image 
            x:Name="MyImage"
            Source="/Assets/ApplicationIcon.png" Height="100" Width="100">
            <Image.RenderTransform>
                <TranslateTransform x:Name="TranslateTransform"/>
            </Image.RenderTransform>
         </Image>
    </Canvas>

在后面的代码中

 private Storyboard GenerateMoveAnimation(double x, double y)
        {
            var xAnimation = new DoubleAnimation
                                 {
                                     From = TranslateTransform.X,
                                     To = x
                                 };

            var yAnimation = new DoubleAnimation
                                 {
                                     From = TranslateTransform.Y,
                                     To = y
                                 };

            Storyboard.SetTarget(xAnimation, TranslateTransform);
            Storyboard.SetTargetProperty(xAnimation, new PropertyPath("X"));

            Storyboard.SetTarget(yAnimation, TranslateTransform);
            Storyboard.SetTargetProperty(yAnimation, new PropertyPath("Y"));

            var str = new Storyboard();
            str.Children.Add(xAnimation);
            str.Children.Add(yAnimation);

            return str;
        }

        private void GestureListnerDragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            var point = e.GetPosition(LayoutRoot);
            GenerateMoveAnimation(point.X, point.Y).Begin();
        }

        private void SurfaceGestureListner_OnTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            var point = e.GetPosition(LayoutRoot);
            GenerateMoveAnimation(point.X, point.Y).Begin();            
        }
于 2013-06-11T17:31:23.120 回答