Animatable.BeginAnimation 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将动画应用于指定的 DependencyProperty。
重载
BeginAnimation(DependencyProperty, AnimationTimeline) |
将动画应用于指定的 DependencyProperty。 当呈现下一帧时,将启动动画。 如果指定的属性已进行动画处理,则使用 SnapshotAndReplace 交接行为。 |
BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior) |
将动画应用于指定的 DependencyProperty。 当呈现下一帧时,将启动动画。 如果指定的属性已进行动画处理,则使用指定的 HandoffBehavior。 |
BeginAnimation(DependencyProperty, AnimationTimeline)
将动画应用于指定的 DependencyProperty。 当呈现下一帧时,将启动动画。 如果指定的属性已进行动画处理,则使用 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)
参数
要进行动画处理的属性。
- animation
- AnimationTimeline
用于对指定属性进行动画处理的动画。
如果动画的 BeginTimenull
,将删除任何当前动画,并且将保留该属性的当前值。
如果 animation
null
,将从属性中删除所有动画,并且属性值将恢复为其基值。
实现
注解
如果动画具有大于零的 BeginTime,则动画将在从下一帧呈现的时间过后开始。
适用于
BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)
将动画应用于指定的 DependencyProperty。 当呈现下一帧时,将启动动画。 如果指定的属性已进行动画处理,则使用指定的 HandoffBehavior。
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)
参数
要进行动画处理的属性。
- animation
- AnimationTimeline
用于对指定属性进行动画处理的动画。
如果 handoffBehavior
SnapshotAndReplace 并且动画的 BeginTimenull
,将删除任何当前动画,并将保留该属性的当前值。
如果 handoffBehavior
SnapshotAndReplace 并且 animation
是 null
引用,则会从该属性中删除所有动画,并且属性值将恢复为其基值。
- handoffBehavior
- HandoffBehavior
一个值,该值指定新动画应如何与任何已影响属性值的当前动画进行交互。
实现
示例
以下示例演示如何使用不同的 HandoffBehavior 设置应用动画。
/*
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
注解
如果动画具有大于零的 BeginTime,则动画将在从下一帧呈现的时间过后开始。
使用 Compose HandoffBehavior
使用 ComposeHandoffBehavior将 Storyboard、AnimationTimeline或 AnimationClock 应用于属性时,以前与该属性关联的任何 Clock 对象将继续使用系统资源;计时系统不会自动删除这些时钟。
若要避免使用 Compose应用大量时钟时出现性能问题,应在动画属性完成后从动画属性中删除撰写时钟。 可通过多种方式删除时钟。
若要从属性中删除所有时钟,请使用动画对象的 ApplyAnimationClock(DependencyProperty, AnimationClock) 或 BeginAnimation(DependencyProperty, AnimationTimeline) 方法。 指定作为第一个参数进行动画处理的属性,
null
为第二个参数。 这将从属性中删除所有动画时钟。若要从时钟列表中删除特定 AnimationClock,请使用 AnimationClock 的 Controller 属性检索 ClockController,然后调用 ClockController的 Remove 方法。 这通常在时钟的 Completed 事件处理程序中完成。 请注意,只有根时钟可由 ClockController控制;子时钟的 Controller 属性将返回
null
。 另请注意,如果时钟的有效持续时间永久,则不会调用 Completed 事件。 在这种情况下,用户需要确定何时调用 Remove。
这主要是对生存期较长的对象进行动画的问题。 当对象被垃圾回收时,其时钟也会断开连接并回收垃圾。
有关时钟对象的详细信息,请参阅 动画和计时系统概述。