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
用來建立指定屬性動畫的動畫。
如果動畫的 BeginTime 是 null
,則會移除任何目前的動畫,並將保留屬性的目前值。
如果 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 且動畫的 BeginTime 是 null
,則會移除任何目前的動畫,並將保留屬性的目前值。
如果 handoffBehavior
SnapshotAndReplace 且 animation
是 null
參考,則會從 屬性中移除所有動畫,而屬性值會還原回其基底值。
如果 handoffBehavior
Compose,則如果動畫或其 BeginTimenull
,這個方法將不會有任何作用。
- 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。
這主要是物件上具有較長存留期之動畫的問題。 當物件被垃圾收集時,其時鐘也會中斷連線並垃圾收集。
如需時鐘物件的詳細資訊,請參閱 動畫和計時系統概觀。