Animatable.BeginAnimation Metoda

Definicja

Stosuje animację do określonego DependencyProperty.

Przeciążenia

BeginAnimation(DependencyProperty, AnimationTimeline)

Stosuje animację do określonego DependencyProperty. Animacja jest uruchamiana po renderowaniu następnej ramki. Jeśli określona właściwość jest już animowana, zostanie użyte zachowanie przekazywania SnapshotAndReplace.

BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)

Stosuje animację do określonego DependencyProperty. Animacja jest uruchamiana po renderowaniu następnej ramki. Jeśli określona właściwość jest już animowana, zostanie użyta określona HandoffBehavior.

BeginAnimation(DependencyProperty, AnimationTimeline)

Stosuje animację do określonego DependencyProperty. Animacja jest uruchamiana po renderowaniu następnej ramki. Jeśli określona właściwość jest już animowana, zostanie użyte zachowanie przekazywania SnapshotAndReplace.

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

Parametry

dp
DependencyProperty

Właściwość do animowania.

animation
AnimationTimeline

Animacja używana do animowania określonej właściwości.

Jeśli BeginTime animacji jest null, wszystkie bieżące animacje zostaną usunięte, a bieżąca wartość właściwości zostanie zatrzymana.

Jeśli animation jest null, wszystkie animacje zostaną usunięte z właściwości, a wartość właściwości powróci do jej wartości podstawowej.

Implementuje

Uwagi

Jeśli animacja ma BeginTime większą niż zero, animacja rozpoczyna się po upływie tego czasu od momentu renderowania następnej ramki.

Dotyczy

.NET Framework 4.8.1 i inne wersje
Produkt Wersje
.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)

Stosuje animację do określonego DependencyProperty. Animacja jest uruchamiana po renderowaniu następnej ramki. Jeśli określona właściwość jest już animowana, zostanie użyta określona HandoffBehavior.

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

Parametry

dp
DependencyProperty

Właściwość do animowania.

animation
AnimationTimeline

Animacja używana do animowania określonej właściwości.

Jeśli handoffBehavior jest SnapshotAndReplace, a BeginTime animacji zostanie null, wszystkie bieżące animacje zostaną usunięte, a bieżąca wartość właściwości zostanie zatrzymana.

Jeśli handoffBehavior jest SnapshotAndReplace, a animation jest odwołaniem null, wszystkie animacje zostaną usunięte z właściwości, a wartość właściwości zostanie przywrócona do jej wartości podstawowej.

Jeśli handoffBehavior jest Compose, ta metoda nie będzie miała wpływu, jeśli animacja lub jej BeginTime jest null.

handoffBehavior
HandoffBehavior

Wartość określająca sposób interakcji nowej animacji z wszelkimi bieżącymi animacjami, które mają już wpływ na wartość właściwości.

Implementuje

Przykłady

W poniższym przykładzie pokazano, jak zastosować animacje przy użyciu różnych ustawień 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;
        }
    }
}

Uwagi

Jeśli animacja ma BeginTime większą niż zero, animacja rozpoczyna się po upływie tego czasu od momentu renderowania następnej ramki.

Korzystanie z narzędzia Compose HandoffBehavior

Zastosowanie Storyboard, AnimationTimelinelub AnimationClock do właściwości przy użyciu ComposeHandoffBehavior, wszystkie obiekty Clock skojarzone wcześniej z tą właściwością nadal zużywają zasoby systemowe; system chronometrażu nie usunie tych zegarów automatycznie.

Aby uniknąć problemów z wydajnością podczas stosowania dużej liczby zegarów przy użyciu Compose, należy usunąć tworzenie zegarów z animowanej właściwości po zakończeniu. Istnieje kilka sposobów usunięcia zegara.

Jest to przede wszystkim problem z animacjami obiektów, które mają długi okres istnienia. Gdy obiekt zostanie odśmiecony, jego zegary również zostaną odłączone i odśmiecane śmieci.

Aby uzyskać więcej informacji o obiektach zegara, zobacz Animacja i System chronometrażu — omówienie.

Dotyczy

.NET Framework 4.8.1 i inne wersje
Produkt Wersje
.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