Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Animatable.BeginAnimation Method (DependencyProperty, AnimationTimeline, HandoffBehavior)

 

Applies an animation to the specified DependencyProperty. The animation is started when the next frame is rendered. If the specified property is already animated, the specified HandoffBehavior is used.

Namespace:   System.Windows.Media.Animation
Assembly:  PresentationCore (in PresentationCore.dll)

Public Sub BeginAnimation (
	dp As DependencyProperty,
	animation As AnimationTimeline,
	handoffBehavior As HandoffBehavior
)

Parameters

dp
Type: System.Windows.DependencyProperty

The property to animate.

animation
Type: System.Windows.Media.Animation.AnimationTimeline

The animation used to animate the specified property.

If handoffBehavior is SnapshotAndReplace and the animation's BeginTime is null, any current animations will be removed and the current value of the property will be held.

If handoffBehavior is SnapshotAndReplace and animation is a null reference, all animations will be removed from the property and the property value will revert back to its base value.

If handoffBehavior is Compose, this method will have no effect if the animation or its BeginTime is null.

handoffBehavior
Type: System.Windows.Media.Animation.HandoffBehavior

A value that specifies how the new animation should interact with any current animations already affecting the property value.

If the animation has a BeginTime that is greater than zero, the animation begins after that amount of time has elapsed from the time the next frame is rendered.

When you apply a Storyboard, AnimationTimeline, or AnimationClock to a property using the Compose HandoffBehavior, any Clock objects previously associated with that property continue to consume system resources; the timing system will not remove these clocks automatically.

To avoid performance issues when you apply a large number of clocks using Compose, you should remove composing clocks from the animated property after they complete. There are several ways to remove a clock.

This is primarily an issue for animations on objects that have a long lifetime. When an object is garbage collected, its clocks will also be disconnected and garbage collected.

For more information about clock objects, see the Animation and Timing System Overview.

The following example shows how to apply animations using different HandoffBehavior settings.

'
'
'   This sample animates the position of an ellipse when 
'   the user clicks within the main border. If the user
'   left-clicks, the SnapshotAndReplace HandoffBehavior
'   is used when applying the animations. If the user
'   right-clicks, the Compose HandoffBehavior is used
'   instead.
'
'

Imports System
Imports System.Windows
Imports System.Windows.Navigation
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports System.Windows.Controls
Imports System.Windows.Input

Namespace Microsoft.Samples.Animation.LocalAnimations


    ' Create the demonstration.
    Public Class InteractiveAnimationExample
    	Inherits Page


        Private Dim interactiveTranslateTransform As TranslateTransform  
        Private Dim WithEvents containerBorder As Border 
        Private Dim interactiveEllipse As Ellipse

        Public Sub New()

            WindowTitle = "Interactive Animation Example"
            Dim myPanel As New DockPanel()
            myPanel.Margin = New Thickness(20.0)            

            containerBorder = new Border()
            containerBorder.Background = Brushes.White
            containerBorder.BorderBrush = Brushes.Black
            containerBorder.BorderThickness = new Thickness(2.0) 
            containerBorder.VerticalAlignment = VerticalAlignment.Stretch

            interactiveEllipse = new Ellipse()
            interactiveEllipse.Fill = Brushes.Lime
            interactiveEllipse.Stroke = Brushes.Black
            interactiveEllipse.StrokeThickness = 2.0
            interactiveEllipse.Width = 25
            interactiveEllipse.Height = 25
            interactiveEllipse.HorizontalAlignment = HorizontalAlignment.Left
            interactiveEllipse.VerticalAlignment = VerticalAlignment.Top

            interactiveTranslateTransform = new TranslateTransform()
            interactiveEllipse.RenderTransform = _
                interactiveTranslateTransform           

            containerBorder.Child = interactiveEllipse
            myPanel.Children.Add(containerBorder)
            Me.Content = myPanel
        End Sub


        ' When the user left-clicks, use the 
        ' SnapshotAndReplace HandoffBehavior when applying the animation.        
       	Private Sub border_mouseLeftButtonDown( _
       		ByVal sender As Object, ByVal e As MouseButtonEventArgs) _
       		Handles containerBorder.MouseLeftButtonDown


            Dim clickPoint = Mouse.GetPosition(containerBorder)

            ' Set the target point so the center of the ellipse
            ' ends up at the clicked point.
            Dim targetPoint As New System.Windows.Point()
            targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2
            targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2  

            ' Animate to the target point.
            Dim xAnimation As _ 
                New DoubleAnimation(targetPoint.X,  _
                New Duration(TimeSpan.FromSeconds(4)))
            interactiveTranslateTransform.BeginAnimation( _
                TranslateTransform.XProperty, xAnimation, HandoffBehavior.SnapshotAndReplace)

            Dim yAnimation As _ 
                New DoubleAnimation(targetPoint.Y, _
                New Duration(TimeSpan.FromSeconds(4)))
            interactiveTranslateTransform.BeginAnimation( _
                TranslateTransform.YProperty, yAnimation, HandoffBehavior.SnapshotAndReplace)                

            ' Change the color of the ellipse.
            interactiveEllipse.Fill = Brushes.Lime

        End Sub

        ' When the user right-clicks, use the 
        ' Compose HandoffBehavior when applying the animation.
        Private Sub border_mouseRightButtonDown( _
        	ByVal sender As Object, ByVal e As MouseButtonEventArgs) _
        	Handles containerBorder.MouseRightButtonDown


            ' Find the point where the use clicked.
            Dim clickPoint = Mouse.GetPosition(containerBorder)

            ' Set the target point so the center of the ellipse
            ' ends up at the clicked point.
            Dim targetPoint As New System.Windows.Point()
            targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2
            targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2

            ' Animate to the target point.
            Dim xAnimation As _
                New DoubleAnimation(targetPoint.X, _
                New Duration(TimeSpan.FromSeconds(4)))
            interactiveTranslateTransform.BeginAnimation( _
                TranslateTransform.XProperty, xAnimation, HandoffBehavior.Compose)

            Dim yAnimation As _ 
                New DoubleAnimation(targetPoint.Y, _
                New Duration(TimeSpan.FromSeconds(4))) 
            interactiveTranslateTransform.BeginAnimation( _
                TranslateTransform.YProperty, yAnimation, HandoffBehavior.Compose)   

            ' Change the color of the ellipse.
            interactiveEllipse.Fill = Brushes.Orange


        End Sub

    End Class

End Namespace

.NET Framework
Available since 3.0
Return to top
Show:
© 2016 Microsoft