Animatable.BeginAnimation Método

Definição

Aplica uma animação ao DependencyPropertyespecificado.

Sobrecargas

BeginAnimation(DependencyProperty, AnimationTimeline)

Aplica uma animação ao DependencyPropertyespecificado. A animação é iniciada quando o próximo quadro é renderizado. Se a propriedade especificada já estiver animada, o comportamento de entrega SnapshotAndReplace será usado.

BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)

Aplica uma animação ao DependencyPropertyespecificado. A animação é iniciada quando o próximo quadro é renderizado. Se a propriedade especificada já estiver animada, o HandoffBehavior especificado será usado.

BeginAnimation(DependencyProperty, AnimationTimeline)

Aplica uma animação ao DependencyPropertyespecificado. A animação é iniciada quando o próximo quadro é renderizado. Se a propriedade especificada já estiver animada, o comportamento de entrega SnapshotAndReplace será usado.

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

Parâmetros

dp
DependencyProperty

A propriedade a ser animada.

animation
AnimationTimeline

A animação usada para animar a propriedade especificada.

Se o BeginTime da animação for null, todas as animações atuais serão removidas e o valor atual da propriedade será mantido.

Se animation for null, todas as animações serão removidas da propriedade e o valor da propriedade será revertido para seu valor base.

Implementações

Comentários

Se a animação tiver um BeginTime maior que zero, a animação começará depois que esse período tiver decorrido a partir do momento em que o próximo quadro for renderizado.

Aplica-se a

.NET Framework 4.8.1 e outras versões
Produto Versões
.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 uma animação ao DependencyPropertyespecificado. A animação é iniciada quando o próximo quadro é renderizado. Se a propriedade especificada já estiver animada, o HandoffBehavior especificado será usado.

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

Parâmetros

dp
DependencyProperty

A propriedade a ser animada.

animation
AnimationTimeline

A animação usada para animar a propriedade especificada.

Se handoffBehavior for SnapshotAndReplace e o BeginTime da animação for null, todas as animações atuais serão removidas e o valor atual da propriedade será mantido.

Se handoffBehavior for SnapshotAndReplace e animation for uma referência null, todas as animações serão removidas da propriedade e o valor da propriedade será revertido para seu valor base.

Se handoffBehavior for Compose, esse método não terá efeito se a animação ou seu BeginTime for null.

handoffBehavior
HandoffBehavior

Um valor que especifica como a nova animação deve interagir com as animações atuais que já afetam o valor da propriedade.

Implementações

Exemplos

O exemplo a seguir mostra como aplicar animações usando configurações de HandoffBehavior diferentes.

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;
        }
    }
}

Comentários

Se a animação tiver um BeginTime maior que zero, a animação começará depois que esse período tiver decorrido a partir do momento em que o próximo quadro for renderizado.

Usando o HandoffBehavior do Compose

Quando você aplica um Storyboard, AnimationTimelineou AnimationClock a uma propriedade usando o ComposeHandoffBehavior, todos os objetos Clock anteriormente associados a essa propriedade continuam a consumir recursos do sistema; o sistema de tempo não removerá esses relógios automaticamente.

Para evitar problemas de desempenho quando você aplica um grande número de relógios usando Compose, remova os relógios de composição da propriedade animada após a conclusão deles. Há várias maneiras de remover um relógio.

Esse é um problema principalmente para animações em objetos que têm um longo tempo de vida. Quando um objeto é coletado, seus relógios também serão desconectados e o lixo coletado.

Para obter mais informações sobre objetos de relógio, consulte a visão geral do sistema de animação e tempo .

Aplica-se a

.NET Framework 4.8.1 e outras versões
Produto Versões
.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