Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use the Completed event of the previous rotation animation to initiate the next rotation animation from the point where the previous animation ended. Here's an example code:

<Grid>
    <Grid.Resources>
        <!-- Define the rotation animation -->
        <Storyboard x:Key="RotationAnimation" RepeatBehavior="Forever">
            <DoubleAnimation From="0" To="360" Duration="0:0:5" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
        </Storyboard>
    </Grid.Resources>

    <Rectangle Width="50" Height="50" Fill="Blue">
        <Rectangle.RenderTransform>
            <RotateTransform/>
        </Rectangle.RenderTransform>
        <!-- Trigger the animation on Loaded event -->
        <Rectangle.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard Storyboard="{StaticResource RotationAnimation}"/>
            </EventTrigger>
        </Rectangle.Triggers>
    </Rectangle>
</Grid>

In this example, the rotation animation is defined in a Storyboard resource with RepeatBehavior="Forever". The Rectangle element has a RenderTransform that uses a RotateTransform. The Loaded event trigger is used to start the rotation animation.

To initiate the next animation from the point where the previous animation ended, you need to handle the Completed event of the rotation animation. Here's an updated code that shows how to do this:

<Grid>
    <Grid.Resources>
        <!-- Define the rotation animation -->
        <Storyboard x:Key="RotationAnimation" Completed="RotationAnimation_Completed">
            <DoubleAnimation From="0" To="360" Duration="0:0:5" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
        </Storyboard>
    </Grid.Resources>

    <Rectangle Width="50" Height="50" Fill="Blue">
        <Rectangle.RenderTransform>
            <RotateTransform/>
        </Rectangle.RenderTransform>
        <!-- Trigger the animation on Loaded event -->
        <Rectangle.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard Storyboard="{StaticResource RotationAnimation}"/>
            </EventTrigger>
        </Rectangle.Triggers>
    </Rectangle>
</Grid>

In the updated code, the Completed event of the rotation animation is handled by the RotationAnimationCompleted method. Here's the implementation of the RotationAnimationCompleted method:

private void RotationAnimation_Completed(object sender, EventArgs e)
{
    // Get the current angle of the rotation transform
    double currentAngle = ((RotateTransform)rectangle.RenderTransform).Angle;

    // Create a new rotation animation starting from the current angle
    DoubleAnimation newAnimation = new DoubleAnimation();
    newAnimation.From = currentAngle;
    newAnimation.To = currentAngle + 360;
    newAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));

    // Start the new animation
    Storyboard.SetTarget(newAnimation, rectangle);
    Storyboard.SetTargetProperty(newAnimation, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
    Storyboard storyboard = new Storyboard();
    storyboard.Children.Add(newAnimation);
    storyboard.Begin();
}

In the RotationAnimation_Completed method, we get the current angle of the rotation transform and use it as the starting angle for the new animation. We create a new DoubleAnimation object and set its From, To, and Duration properties. We then use the SetTarget and SetTargetProperty methods to set the target element and property for the animation. Finally, we create a new Storyboard object, add the new animation to it, and call the Begin method to start the animation.