Animatable.BeginAnimation Método

Definición

Aplica una animación al DependencyPropertyespecificado.

Sobrecargas

BeginAnimation(DependencyProperty, AnimationTimeline)

Aplica una animación al DependencyPropertyespecificado. La animación se inicia cuando se representa el siguiente fotograma. Si la propiedad especificada ya está animada, se usa el SnapshotAndReplace comportamiento de entrega.

BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)

Aplica una animación al DependencyPropertyespecificado. La animación se inicia cuando se representa el siguiente fotograma. Si la propiedad especificada ya está animada, se usa el HandoffBehavior especificado.

BeginAnimation(DependencyProperty, AnimationTimeline)

Aplica una animación al DependencyPropertyespecificado. La animación se inicia cuando se representa el siguiente fotograma. Si la propiedad especificada ya está animada, se usa el SnapshotAndReplace comportamiento de entrega.

C#
public void BeginAnimation (System.Windows.DependencyProperty dp, System.Windows.Media.Animation.AnimationTimeline animation);

Parámetros

dp
DependencyProperty

Propiedad que se va a animar.

animation
AnimationTimeline

Animación utilizada para animar la propiedad especificada.

Si el BeginTime de la animación es null, se quitarán las animaciones actuales y se mantendrá el valor actual de la propiedad.

Si animation es null, todas las animaciones se quitarán de la propiedad y el valor de la propiedad volverá a su valor base.

Implementaciones

Comentarios

Si la animación tiene un BeginTime mayor que cero, la animación comienza después de que haya transcurrido esa cantidad de tiempo desde el momento en que se representa el siguiente fotograma.

Se aplica a

.NET Framework 4.8.1 y otras versiones
Producto Versiones
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)

Aplica una animación al DependencyPropertyespecificado. La animación se inicia cuando se representa el siguiente fotograma. Si la propiedad especificada ya está animada, se usa el HandoffBehavior especificado.

C#
public void BeginAnimation (System.Windows.DependencyProperty dp, System.Windows.Media.Animation.AnimationTimeline animation, System.Windows.Media.Animation.HandoffBehavior handoffBehavior);

Parámetros

dp
DependencyProperty

Propiedad que se va a animar.

animation
AnimationTimeline

Animación utilizada para animar la propiedad especificada.

Si handoffBehavior es SnapshotAndReplace y el BeginTime de la animación es null, se quitarán las animaciones actuales y se mantendrá el valor actual de la propiedad.

Si handoffBehavior es SnapshotAndReplace y animation es una referencia de null, todas las animaciones se quitarán de la propiedad y el valor de propiedad volverá a su valor base.

Si handoffBehavior es Compose, este método no tendrá ningún efecto si la animación o su BeginTime es null.

handoffBehavior
HandoffBehavior

Valor que especifica cómo la nueva animación debe interactuar con cualquier animación actual que ya afecte al valor de propiedad.

Implementaciones

Ejemplos

En el ejemplo siguiente se muestra cómo aplicar animaciones mediante diferentes configuraciones de HandoffBehavior.

C#
/*

   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.

*/

using System;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Input;

namespace Microsoft.Samples.Animation.LocalAnimations
{

    // Create the demonstration.
    public class InteractiveAnimationExample : Page {

        private TranslateTransform interactiveTranslateTransform;   
        private Border containerBorder;
        private Ellipse interactiveEllipse;
        
        public InteractiveAnimationExample()
        {
        
            WindowTitle = "Interactive Animation Example";
            DockPanel myPanel = new DockPanel();
            myPanel.Margin = new Thickness(20.0);            
    
            containerBorder = new Border();
            containerBorder.Background = System.Windows.Media.Brushes.White;
            containerBorder.BorderBrush = System.Windows.Media.Brushes.Black;
            containerBorder.BorderThickness = new Thickness(2.0); 
            containerBorder.VerticalAlignment = VerticalAlignment.Stretch;
            
            interactiveEllipse = new Ellipse();
            interactiveEllipse.Fill = System.Windows.Media.Brushes.Lime;
            interactiveEllipse.Stroke = System.Windows.Media.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.MouseLeftButtonDown += 
                new MouseButtonEventHandler(border_mouseLeftButtonDown);
            containerBorder.MouseRightButtonDown += 
                new MouseButtonEventHandler(border_mouseRightButtonDown);                
            
            containerBorder.Child = interactiveEllipse;
            myPanel.Children.Add(containerBorder);
            this.Content = myPanel;
        }

        // When the user left-clicks, use the 
        // SnapshotAndReplace HandoffBehavior when applying the animation.        
        private void border_mouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        
            System.Windows.Point clickPoint = Mouse.GetPosition(containerBorder);
            
            // Set the target point so the center of the ellipse
            // ends up at the clicked point.
            System.Windows.Point targetPoint = new System.Windows.Point();
            targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2;
            targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2;  
            
            // Animate to the target point.
            DoubleAnimation xAnimation = 
                new DoubleAnimation(targetPoint.X, 
                new Duration(TimeSpan.FromSeconds(4)));
            interactiveTranslateTransform.BeginAnimation(
                TranslateTransform.XProperty, xAnimation, HandoffBehavior.SnapshotAndReplace);
                
            DoubleAnimation yAnimation = 
                new DoubleAnimation(targetPoint.Y, 
                new Duration(TimeSpan.FromSeconds(4))); 
            interactiveTranslateTransform.BeginAnimation(
                TranslateTransform.YProperty, yAnimation, HandoffBehavior.SnapshotAndReplace);                

            // Change the color of the ellipse.
            interactiveEllipse.Fill = System.Windows.Media.Brushes.Lime;
        }
        
        // When the user right-clicks, use the 
        // Compose HandoffBehavior when applying the animation.
        private void border_mouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
        
            // Find the point where the use clicked.
            System.Windows.Point clickPoint = Mouse.GetPosition(containerBorder);
            
            // Set the target point so the center of the ellipse
            // ends up at the clicked point.
            System.Windows.Point targetPoint = new System.Windows.Point();
            targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2;
            targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2;

            // Animate to the target point.
            DoubleAnimation xAnimation = 
                new DoubleAnimation(targetPoint.X, 
                new Duration(TimeSpan.FromSeconds(4)));
            interactiveTranslateTransform.BeginAnimation(
                TranslateTransform.XProperty, xAnimation, HandoffBehavior.Compose);
                
            DoubleAnimation yAnimation = 
                new DoubleAnimation(targetPoint.Y, 
                new Duration(TimeSpan.FromSeconds(4))); 
            interactiveTranslateTransform.BeginAnimation(
                TranslateTransform.YProperty, yAnimation, HandoffBehavior.Compose);   
                
            // Change the color of the ellipse.
            interactiveEllipse.Fill = System.Windows.Media.Brushes.Orange;
        }
    }
}

Comentarios

Si la animación tiene un BeginTime mayor que cero, la animación comienza después de que haya transcurrido esa cantidad de tiempo desde el momento en que se representa el siguiente fotograma.

Uso de Compose HandoffBehavior

Cuando se aplica un Storyboard, AnimationTimelineo AnimationClock a una propiedad mediante el ComposeHandoffBehavior, los objetos Clock asociados previamente a esa propiedad siguen usando recursos del sistema; el sistema de control de tiempo no quitará estos relojes automáticamente.

Para evitar problemas de rendimiento al aplicar un gran número de relojes mediante Compose, debe quitar la composición de los relojes de la propiedad animada después de completarlos. Hay varias maneras de quitar un reloj.

Esto es principalmente un problema para animaciones en objetos que tienen una larga duración. Cuando se recolecte un objeto, sus relojes también se desconectarán y se recopilarán elementos no utilizados.

Para obtener más información sobre los objetos de reloj, vea la Animación e Información general del sistema de tiempo.

Se aplica a

.NET Framework 4.8.1 y otras versiones
Producto Versiones
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9