Animatable.BeginAnimation Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Aplica una animación a la DependencyProperty especificada.
Sobrecargas
BeginAnimation(DependencyProperty, AnimationTimeline) |
Aplica una animación a la DependencyProperty especificada. La animación se inicia al representar el siguiente fotograma. Si la propiedad especificada ya está animada, se usa el comportamiento de entrega SnapshotAndReplace. |
BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior) |
Aplica una animación a la DependencyProperty especificada. La animación se inicia al representar el siguiente fotograma. Si la propiedad especificada ya está animada, se usa el objeto HandoffBehavior especificado. |
BeginAnimation(DependencyProperty, AnimationTimeline)
Aplica una animación a la DependencyProperty especificada. La animación se inicia al representar el siguiente fotograma. Si la propiedad especificada ya está animada, se usa el comportamiento de entrega SnapshotAndReplace.
public:
virtual void BeginAnimation(System::Windows::DependencyProperty ^ dp, System::Windows::Media::Animation::AnimationTimeline ^ animation);
public void BeginAnimation (System.Windows.DependencyProperty dp, System.Windows.Media.Animation.AnimationTimeline animation);
abstract member BeginAnimation : System.Windows.DependencyProperty * System.Windows.Media.Animation.AnimationTimeline -> unit
override this.BeginAnimation : System.Windows.DependencyProperty * System.Windows.Media.Animation.AnimationTimeline -> unit
Public Sub BeginAnimation (dp As DependencyProperty, animation As AnimationTimeline)
Parámetros
Propiedad que se va a animar.
- animation
- AnimationTimeline
Animación usada para animar la propiedad especificada.
Si el valor BeginTime de la animación es null
, se quitarán las animaciones actuales y se conservará 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 se restablecerá a su valor base.
Implementaciones
Comentarios
Si la animación tiene un BeginTime valor mayor que cero, la animación comienza después de que haya transcurrido esa cantidad de tiempo desde el momento en que se represente el siguiente fotograma.
Se aplica a
BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)
Aplica una animación a la DependencyProperty especificada. La animación se inicia al representar el siguiente fotograma. Si la propiedad especificada ya está animada, se usa el objeto HandoffBehavior especificado.
public:
virtual void BeginAnimation(System::Windows::DependencyProperty ^ dp, System::Windows::Media::Animation::AnimationTimeline ^ animation, System::Windows::Media::Animation::HandoffBehavior handoffBehavior);
public void BeginAnimation (System.Windows.DependencyProperty dp, System.Windows.Media.Animation.AnimationTimeline animation, System.Windows.Media.Animation.HandoffBehavior handoffBehavior);
abstract member BeginAnimation : System.Windows.DependencyProperty * System.Windows.Media.Animation.AnimationTimeline * System.Windows.Media.Animation.HandoffBehavior -> unit
override this.BeginAnimation : System.Windows.DependencyProperty * System.Windows.Media.Animation.AnimationTimeline * System.Windows.Media.Animation.HandoffBehavior -> unit
Public Sub BeginAnimation (dp As DependencyProperty, animation As AnimationTimeline, handoffBehavior As HandoffBehavior)
Parámetros
Propiedad que se va a animar.
- animation
- AnimationTimeline
Animación usada para animar la propiedad especificada.
Si handoffBehavior
es SnapshotAndReplace y la propiedad BeginTime de la animación es null
, se quitarán las animaciones actuales y se conservará 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 su valor se restablecerá a su valor base.
Si handoffBehavior
es Compose, este método no tendrá efecto si el valor de la animación o de su propiedad BeginTime es null
.
- handoffBehavior
- HandoffBehavior
Valor que especifica la interacción de la nueva animación con cualquier animación actual que ya afecte el valor de propiedad.
Implementaciones
Ejemplos
En el ejemplo siguiente se muestra cómo aplicar animaciones mediante diferentes HandoffBehavior configuraciones.
/*
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 namespace System;
using namespace System::Windows;
using namespace System::Windows::Navigation;
using namespace System::Windows::Media;
using namespace System::Windows::Media::Animation;
using namespace System::Windows::Shapes;
using namespace System::Windows::Controls;
using namespace System::Windows::Input;
namespace Microsoft {
namespace Samples {
namespace Animation {
namespace LocalAnimations {
public ref class InteractiveAnimationExample : Page {
private:
TranslateTransform^ interactiveTranslateTransform;
Border^ containerBorder;
Ellipse^ interactiveEllipse;
public:
InteractiveAnimationExample ()
{
WindowTitle = "Interactive Animation Example";
DockPanel^ myPanel = gcnew DockPanel();
myPanel->Margin = Thickness(20.0);
containerBorder = gcnew Border();
containerBorder->Background = Brushes::White;
containerBorder->BorderBrush = Brushes::Black;
containerBorder->BorderThickness = Thickness(2.0);
containerBorder->VerticalAlignment = System::Windows::VerticalAlignment::Stretch;
interactiveEllipse = gcnew Ellipse();
interactiveEllipse->Fill = Brushes::Lime;
interactiveEllipse->Stroke = Brushes::Black;
interactiveEllipse->StrokeThickness = 2.0;
interactiveEllipse->Width = 25;
interactiveEllipse->Height = 25;
interactiveEllipse->HorizontalAlignment = System::Windows::HorizontalAlignment::Left;
interactiveEllipse->VerticalAlignment = System::Windows::VerticalAlignment::Top;
interactiveTranslateTransform = gcnew TranslateTransform();
interactiveEllipse->RenderTransform = interactiveTranslateTransform;
containerBorder->MouseLeftButtonDown +=
gcnew MouseButtonEventHandler(this, &Microsoft::Samples::Animation::LocalAnimations::InteractiveAnimationExample::border_mouseLeftButtonDown);
containerBorder->MouseRightButtonDown +=
gcnew MouseButtonEventHandler(this, &Microsoft::Samples::Animation::LocalAnimations::InteractiveAnimationExample::border_mouseRightButtonDown);
containerBorder->Child = interactiveEllipse;
myPanel->Children->Add(containerBorder);
this->Content = myPanel;
};
private:
// When the user left-clicks, use the
// SnapshotAndReplace HandoffBehavior when applying the animation.
void border_mouseLeftButtonDown (System::Object^ sender, System::Windows::Input::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.
Point targetPoint = Point();
targetPoint.X = clickPoint.X - interactiveEllipse->Width / 2;
targetPoint.Y = clickPoint.Y - interactiveEllipse->Height / 2;
// Animate to the target point.
DoubleAnimation^ xAnimation = gcnew DoubleAnimation(targetPoint.X,
Duration(TimeSpan::FromSeconds(4)));
interactiveTranslateTransform->BeginAnimation(TranslateTransform::XProperty, xAnimation, HandoffBehavior::SnapshotAndReplace);
DoubleAnimation^ yAnimation = gcnew DoubleAnimation(targetPoint.Y,
Duration(TimeSpan::FromSeconds(4)));
interactiveTranslateTransform->BeginAnimation(TranslateTransform::YProperty, yAnimation, HandoffBehavior::SnapshotAndReplace);
// Chage the color of the ellipse.
interactiveEllipse->Fill = Brushes::Lime;
}
private:
// When the user right-clicks, use the
// Compose HandoffBehavior when applying the animation.
void border_mouseRightButtonDown (System::Object^ sender, System::Windows::Input::MouseButtonEventArgs^ e)
{
// Find the point where the use clicked.
Point clickPoint = Mouse::GetPosition(containerBorder);
// Set the target point so the center of the ellipse
// ends up at the clicked point.
Point targetPoint = System::Windows::Point();
targetPoint.X = clickPoint.X - interactiveEllipse->Width / 2;
targetPoint.Y = clickPoint.Y - interactiveEllipse->Height / 2;
// Animate to the target point.
DoubleAnimation^ xAnimation = gcnew DoubleAnimation(targetPoint.X,
Duration(TimeSpan::FromSeconds(4)));
interactiveTranslateTransform->BeginAnimation(TranslateTransform::XProperty, xAnimation, HandoffBehavior::Compose);
DoubleAnimation^ yAnimation = gcnew DoubleAnimation(targetPoint.Y,
Duration(TimeSpan::FromSeconds(4)));
// Change the color of the ellipse.
interactiveTranslateTransform->BeginAnimation(TranslateTransform::YProperty, yAnimation, HandoffBehavior::Compose);
interactiveEllipse->Fill = Brushes::Orange;
}
};
}
}
}
}
/*
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;
}
}
}
'
'
' 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.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
Comentarios
Si la animación tiene un BeginTime valor mayor que cero, la animación comienza después de que haya transcurrido esa cantidad de tiempo desde el momento en que se represente el siguiente fotograma.
Uso de Compose HandoffBehavior
Cuando se aplica un objeto Storyboard, AnimationTimeline o AnimationClock a una propiedad utilizando el valor Compose de HandoffBehavior, los objetos Clock asociados con anterioridad a esa propiedad siguen utilizando recursos del sistema; el sistema de control de tiempo no quitará estos relojes automáticamente.
Para evitar problemas de rendimiento cuando aplique muchos relojes mediante Compose, debe quitar los relojes de composición de la propiedad animada cuando se hayan completado. Hay varias formas de quitar un reloj.
Para quitar todos los relojes de una propiedad, utilice el método ApplyAnimationClock(DependencyProperty, AnimationClock) o BeginAnimation(DependencyProperty, AnimationTimeline) del objeto animado. Especifique la propiedad que se va a animar como primer parámetro y
null
como segundo parámetro. De este modo, se quitarán todos los relojes de animación de la propiedad.Para quitar un elemento específico AnimationClock de una lista de relojes, use la propiedad Controller de AnimationClock para recuperar un objeto ClockController y, a continuación, llame al método Remove de ClockController. Normalmente esta operación se realiza en el controlador de eventos Completed de un reloj. Observe que solo ClockController puede controlar los relojes de raíz; la propiedad Controller de un reloj secundario devolverá
null
. Tenga en cuenta también que no se provocará el evento Completed si la duración real del reloj es para siempre. En ese caso, el usuario deberá determinar cuándo llamar a Remove.
Este problema se produce principalmente en las animaciones de objetos que tienen un período de duración prolongado. Cuando un objeto se recolecta como elemento no utilizado, sus relojes también se desconectan y se recolectan como elementos no utilizados.
Para obtener más información sobre los objetos clock, vea Información general sobre el sistema de control de tiempo y animación.
Se aplica a
Comentarios
https://aka.ms/ContentUserFeedback.
Próximamente: A lo largo de 2024 iremos eliminando gradualmente GitHub Issues como mecanismo de comentarios sobre el contenido y lo sustituiremos por un nuevo sistema de comentarios. Para más información, vea:Enviar y ver comentarios de