Animatable.BeginAnimation Méthode

Définition

Applique une animation au DependencyPropertyspécifié.

Surcharges

BeginAnimation(DependencyProperty, AnimationTimeline)

Applique une animation au DependencyPropertyspécifié. L’animation est démarrée lorsque l’image suivante est rendue. Si la propriété spécifiée est déjà animée, le comportement de transfert SnapshotAndReplace est utilisé.

BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)

Applique une animation au DependencyPropertyspécifié. L’animation est démarrée lorsque l’image suivante est rendue. Si la propriété spécifiée est déjà animée, la HandoffBehavior spécifiée est utilisée.

BeginAnimation(DependencyProperty, AnimationTimeline)

Applique une animation au DependencyPropertyspécifié. L’animation est démarrée lorsque l’image suivante est rendue. Si la propriété spécifiée est déjà animée, le comportement de transfert SnapshotAndReplace est utilisé.

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

Paramètres

dp
DependencyProperty

Propriété à animer.

animation
AnimationTimeline

Animation utilisée pour animer la propriété spécifiée.

Si la BeginTime de l’animation est null, toutes les animations actuelles sont supprimées et la valeur actuelle de la propriété est conservée.

Si animation est null, toutes les animations sont supprimées de la propriété et la valeur de la propriété revient à sa valeur de base.

Implémente

Remarques

Si l’animation a une BeginTime supérieure à zéro, l’animation commence après ce temps écoulé à partir du moment où l’image suivante est rendue.

S’applique à

.NET Framework 4.8.1 et autres versions
Produit Versions
.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)

Applique une animation au DependencyPropertyspécifié. L’animation est démarrée lorsque l’image suivante est rendue. Si la propriété spécifiée est déjà animée, la HandoffBehavior spécifiée est utilisée.

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

Paramètres

dp
DependencyProperty

Propriété à animer.

animation
AnimationTimeline

Animation utilisée pour animer la propriété spécifiée.

Si handoffBehavior est SnapshotAndReplace et que le BeginTime de l’animation est null, toutes les animations actuelles sont supprimées et la valeur actuelle de la propriété est conservée.

Si handoffBehavior est SnapshotAndReplace et animation est une référence null, toutes les animations sont supprimées de la propriété et la valeur de la propriété revient à sa valeur de base.

Si handoffBehavior est Compose, cette méthode n’aura aucun effet si l’animation ou son BeginTime est null.

handoffBehavior
HandoffBehavior

Valeur qui spécifie la façon dont la nouvelle animation doit interagir avec toutes les animations actuelles affectant déjà la valeur de la propriété.

Implémente

Exemples

L’exemple suivant montre comment appliquer des animations à l’aide de différents paramètres 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;
        }
    }
}

Remarques

Si l’animation a une BeginTime supérieure à zéro, l’animation commence après ce temps écoulé à partir du moment où l’image suivante est rendue.

Utilisation de Compose HandoffBehavior

Lorsque vous appliquez un Storyboard, AnimationTimelineou AnimationClock à une propriété à l’aide du ComposeHandoffBehavior, tous les objets Clock précédemment associés à cette propriété continuent à consommer des ressources système ; le système de minutage ne supprime pas automatiquement ces horloges.

Pour éviter les problèmes de performances lorsque vous appliquez un grand nombre d’horloges à l’aide de Compose, vous devez supprimer la composition d’horloges de la propriété animée une fois qu’elles ont été terminées. Il existe plusieurs façons de supprimer une horloge.

Il s’agit principalement d’un problème pour les animations sur les objets qui ont une longue durée de vie. Lorsqu’un objet est récupéré par la mémoire, ses horloges sont également déconnectées et récupérées par le garbage.

Pour plus d’informations sur les objets horloge, consultez la Vue d’ensemble de l’animation et du système de minutage.

S’applique à

.NET Framework 4.8.1 et autres versions
Produit Versions
.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