在画布中停止故事板

时间:2016-06-07 12:41:22

标签: c# wpf

我有以下非常简单的设置。

<Window x:Class="WpfApplication5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
    Title="MainWindow" Height="800" Width="1200">
<Window.Resources>
    <Storyboard x:Key="Storyboard1">
        <DoubleAnimationUsingPath 
            Duration="0:0:5" Source="X" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)" >
            <DoubleAnimationUsingPath.PathGeometry>
                <PathGeometry Figures="M-348.26805,-1.3505859E-05 L66.500091,-1.3505859E-05"/>
            </DoubleAnimationUsingPath.PathGeometry>
        </DoubleAnimationUsingPath>
        <DoubleAnimationUsingPath Duration="0:0:5" Source="Y" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)" >
            <DoubleAnimationUsingPath.PathGeometry>
                <PathGeometry Figures="M-348.26805,-1.3505859E-05 L66.500091,-1.3505859E-05"/>
            </DoubleAnimationUsingPath.PathGeometry>
        </DoubleAnimationUsingPath>
    </Storyboard>
</Window.Resources>

<Grid>
    <Canvas x:Name="Animation_Path" Left="3.409" Top="53.412" Margin="202.428,351.761,482.635,360.19" RenderTransformOrigin="0.5,0.5">
        <Path x:Name="testpath" Data="M683.91667,287.08333 L816.91635,287.08333" Height="0" Canvas.Left="683.917" Stretch="Fill" Canvas.Top="287.083" Margin="247.732,384.5,529.5,384.5"/>

    </Canvas>

    <Button Content="Start" HorizontalAlignment="Left" Margin="588,252,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_5"/>
    <Button Content="Stop" HorizontalAlignment="Left" Margin="690,252,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_6"/>

</Grid>

背后的代码是这样的。

   private void Button_Click_5(object sender, RoutedEventArgs e)

    {
        ObjectToMove move = new ObjectToMove();
        Animation_Path.Children.Add(move);

        var sb1 = FindResource("Storyboard1") as Storyboard;
        sb1.Begin(move);

    }

这很好用。每次单击按钮时,我都可以运行动画并创建新对象。但问题是我愿意在某个必要的点停止动画。

使用sb1.Stop()并没有做到这一点。我认为这是必须的,因为我将新创建的对象作为子项添加到animation_path,这就是为什么我开始时没有控制权。

所以问题是:

在这种情况下是否可以停止动画..?

最好的改变选择是什么??

0 个答案:

没有答案