Storyboard.Begin 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
起始與此 Storyboard相關聯的動畫集。
多載
備註
可控制的分鏡腳本可以暫停、繼續、搜尋、停止及移除。 若要在程式代碼中控制分鏡腳本,您必須使用腳本 Begin 方法的適當多載,並指定 true
使其可控制。 如需範例,請參閱 How to: Control a Storyboard after it starts。
Begin(FrameworkElement, FrameworkTemplate, HandoffBehavior, Boolean)
將與此 Storyboard 相關聯的動畫套用至指定範本內的目標,並加以起始。
public:
void Begin(System::Windows::FrameworkElement ^ containingObject, System::Windows::FrameworkTemplate ^ frameworkTemplate, System::Windows::Media::Animation::HandoffBehavior handoffBehavior, bool isControllable);
public void Begin (System.Windows.FrameworkElement containingObject, System.Windows.FrameworkTemplate frameworkTemplate, System.Windows.Media.Animation.HandoffBehavior handoffBehavior, bool isControllable);
member this.Begin : System.Windows.FrameworkElement * System.Windows.FrameworkTemplate * System.Windows.Media.Animation.HandoffBehavior * bool -> unit
Public Sub Begin (containingObject As FrameworkElement, frameworkTemplate As FrameworkTemplate, handoffBehavior As HandoffBehavior, isControllable As Boolean)
參數
- containingObject
- FrameworkElement
套用指定 frameworkTemplate
的物件。 沒有 TargetName 的動畫會套用至 containingObject
。
- frameworkTemplate
- FrameworkTemplate
要產生動畫效果的範本。
- handoffBehavior
- HandoffBehavior
新動畫應該用來與任何目前動畫互動的行為。
- isControllable
- Boolean
如果分鏡腳本應該以互動方式控制,true
;否則,false
。
備註
若要以互動方式控制這個分鏡腳本,您必須在呼叫用來開始分鏡腳本的互動式方法時,指定相同的 containingObject
呼叫此方法時,會為分鏡腳本建立 Clock 物件及其包含的任何時間軸。 這些時鐘會與 containingObject
一起儲存。
開始分鏡腳本會觸發 CurrentStateInvalidated 和 CurrentGlobalSpeedInvalidated 事件。
使用 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。
這主要是物件上具有較長存留期之動畫的問題。 當物件被垃圾收集時,其時鐘也會中斷連線並垃圾收集。
如需時鐘物件的詳細資訊,請參閱 動畫和計時系統概觀。
適用於
Begin(FrameworkElement, HandoffBehavior, Boolean)
將與此 Storyboard 相關聯的動畫套用至其目標並加以起始。
public:
void Begin(System::Windows::FrameworkElement ^ containingObject, System::Windows::Media::Animation::HandoffBehavior handoffBehavior, bool isControllable);
public void Begin (System.Windows.FrameworkElement containingObject, System.Windows.Media.Animation.HandoffBehavior handoffBehavior, bool isControllable);
member this.Begin : System.Windows.FrameworkElement * System.Windows.Media.Animation.HandoffBehavior * bool -> unit
Public Sub Begin (containingObject As FrameworkElement, handoffBehavior As HandoffBehavior, isControllable As Boolean)
參數
- containingObject
- FrameworkElement
物件包含在與這個分鏡腳本動畫目標相同的名稱範圍內。 沒有指定 TargetName 的動畫會套用至 containingObject
。
- handoffBehavior
- HandoffBehavior
新動畫應該用來與任何目前動畫互動的行為。
- isControllable
- Boolean
宣告動畫是否可控制(可暫停)一旦啟動。
範例
下列範例示範如何建立可控制的分鏡腳本。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace SDKSample
{
public class ControllableStoryboardExample : Page
{
private Storyboard myStoryboard;
public ControllableStoryboardExample()
{
// Create a name scope for the page.
NameScope.SetNameScope(this, new NameScope());
this.WindowTitle = "Controllable Storyboard Example";
StackPanel myStackPanel = new StackPanel();
myStackPanel.Margin = new Thickness(10);
// Create a rectangle.
Rectangle myRectangle = new Rectangle();
myRectangle.Name = "myRectangle";
// Assign the rectangle a name by
// registering it with the page, so that
// it can be targeted by storyboard
// animations.
this.RegisterName(myRectangle.Name, myRectangle);
myRectangle.Width = 100;
myRectangle.Height = 100;
myRectangle.Fill = Brushes.Blue;
myStackPanel.Children.Add(myRectangle);
//
// Create an animation and a storyboard to animate the
// rectangle.
//
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 1.0;
myDoubleAnimation.To = 0.0;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(5000));
myDoubleAnimation.AutoReverse = true;
// Create the storyboard.
myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);
Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.OpacityProperty));
//
// Create some buttons to control the storyboard
// and a panel to contain them.
//
StackPanel buttonPanel = new StackPanel();
buttonPanel.Orientation = Orientation.Horizontal;
Button beginButton = new Button();
beginButton.Content = "Begin";
beginButton.Click += new RoutedEventHandler(beginButton_Clicked);
buttonPanel.Children.Add(beginButton);
Button pauseButton = new Button();
pauseButton.Content = "Pause";
pauseButton.Click += new RoutedEventHandler(pauseButton_Clicked);
buttonPanel.Children.Add(pauseButton);
Button resumeButton = new Button();
resumeButton.Content = "Resume";
resumeButton.Click += new RoutedEventHandler(resumeButton_Clicked);
buttonPanel.Children.Add(resumeButton);
Button skipToFillButton = new Button();
skipToFillButton.Content = "Skip to Fill";
skipToFillButton.Click += new RoutedEventHandler(skipToFillButton_Clicked);
buttonPanel.Children.Add(skipToFillButton);
Button setSpeedRatioButton = new Button();
setSpeedRatioButton.Content = "Triple Speed";
setSpeedRatioButton.Click += new RoutedEventHandler(setSpeedRatioButton_Clicked);
buttonPanel.Children.Add(setSpeedRatioButton);
Button stopButton = new Button();
stopButton.Content = "Stop";
stopButton.Click += new RoutedEventHandler(stopButton_Clicked);
buttonPanel.Children.Add(stopButton);
myStackPanel.Children.Add(buttonPanel);
this.Content = myStackPanel;
}
// Begins the storyboard.
private void beginButton_Clicked(object sender, RoutedEventArgs args)
{
// Specifying "true" as the second Begin parameter
// makes this storyboard controllable.
myStoryboard.Begin(this, true);
}
// Pauses the storyboard.
private void pauseButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Pause(this);
}
// Resumes the storyboard.
private void resumeButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Resume(this);
}
// Advances the storyboard to its fill period.
private void skipToFillButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.SkipToFill(this);
}
// Updates the storyboard's speed.
private void setSpeedRatioButton_Clicked(object sender, RoutedEventArgs args)
{
// Makes the storyboard progress three times as fast as normal.
myStoryboard.SetSpeedRatio(this, 3);
}
// Stops the storyboard.
private void stopButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Stop(this);
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Shapes
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Namespace SDKSample
Public Class ControllableStoryboardExample
Inherits Page
Private myStoryboard As Storyboard
Public Sub New()
' Create a name scope for the page.
NameScope.SetNameScope(Me, New NameScope())
Me.WindowTitle = "Controllable Storyboard Example"
Dim myStackPanel As New StackPanel()
myStackPanel.Margin = New Thickness(10)
' Create a rectangle.
Dim myRectangle As New Rectangle()
myRectangle.Name = "myRectangle"
' Assign the rectangle a name by
' registering it with the page, so that
' it can be targeted by storyboard
' animations.
Me.RegisterName(myRectangle.Name, myRectangle)
myRectangle.Width = 100
myRectangle.Height = 100
myRectangle.Fill = Brushes.Blue
myStackPanel.Children.Add(myRectangle)
'
' Create an animation and a storyboard to animate the
' rectangle.
'
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.From = 1.0
myDoubleAnimation.To = 0.0
myDoubleAnimation.Duration = New Duration(TimeSpan.FromMilliseconds(5000))
myDoubleAnimation.AutoReverse = True
' Create the storyboard.
myStoryboard = New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)
Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name)
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.OpacityProperty))
'
' Create some buttons to control the storyboard
' and a panel to contain them.
'
Dim buttonPanel As New StackPanel()
buttonPanel.Orientation = Orientation.Horizontal
Dim beginButton As New Button()
beginButton.Content = "Begin"
AddHandler beginButton.Click, AddressOf beginButton_Clicked
buttonPanel.Children.Add(beginButton)
Dim pauseButton As New Button()
pauseButton.Content = "Pause"
AddHandler pauseButton.Click, AddressOf pauseButton_Clicked
buttonPanel.Children.Add(pauseButton)
Dim resumeButton As New Button()
resumeButton.Content = "Resume"
AddHandler resumeButton.Click, AddressOf resumeButton_Clicked
buttonPanel.Children.Add(resumeButton)
Dim skipToFillButton As New Button()
skipToFillButton.Content = "Skip to Fill"
AddHandler skipToFillButton.Click, AddressOf skipToFillButton_Clicked
buttonPanel.Children.Add(skipToFillButton)
Dim setSpeedRatioButton As New Button()
setSpeedRatioButton.Content = "Triple Speed"
AddHandler setSpeedRatioButton.Click, AddressOf setSpeedRatioButton_Clicked
buttonPanel.Children.Add(setSpeedRatioButton)
Dim stopButton As New Button()
stopButton.Content = "Stop"
AddHandler stopButton.Click, AddressOf stopButton_Clicked
buttonPanel.Children.Add(stopButton)
myStackPanel.Children.Add(buttonPanel)
Me.Content = myStackPanel
End Sub
' Begins the storyboard.
Private Sub beginButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
' Specifying "true" as the second Begin parameter
' makes this storyboard controllable.
myStoryboard.Begin(Me, True)
End Sub
' Pauses the storyboard.
Private Sub pauseButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Pause(Me)
End Sub
' Resumes the storyboard.
Private Sub resumeButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Resume(Me)
End Sub
' Advances the storyboard to its fill period.
Private Sub skipToFillButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.SkipToFill(Me)
End Sub
' Updates the storyboard's speed.
Private Sub setSpeedRatioButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
' Makes the storyboard progress three times as fast as normal.
myStoryboard.SetSpeedRatio(Me, 3)
End Sub
' Stops the storyboard.
Private Sub stopButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Stop(Me)
End Sub
End Class
End Namespace
下一個範例會使用 SnapshotAndReplaceHandoffBehavior,在使用者按兩下滑鼠左鍵時產生動畫效果,並在使用者按兩下滑鼠右鍵時 ComposeHandoffBehavior。
/*
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.AnimatingWithStoryboards
{
// Create the demonstration.
public class FrameworkElementStoryboardHandoffBehaviorExample : Page {
private Border containerBorder;
private Ellipse interactiveEllipse;
private Storyboard theStoryboard;
private DoubleAnimation xAnimation;
private DoubleAnimation yAnimation;
public FrameworkElementStoryboardHandoffBehaviorExample()
{
WindowTitle = "Interactive Animation Example";
// Create a name scope for the page.
NameScope.SetNameScope(this, new NameScope());
DockPanel myPanel = 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;
TranslateTransform interactiveTranslateTransform = new TranslateTransform();
this.RegisterName("InteractiveTranslateTransform", interactiveTranslateTransform);
interactiveEllipse.RenderTransform =
interactiveTranslateTransform;
xAnimation = new DoubleAnimation();
xAnimation.Duration = TimeSpan.FromSeconds(4);
yAnimation = xAnimation.Clone();
Storyboard.SetTargetName(xAnimation, "InteractiveTranslateTransform");
Storyboard.SetTargetProperty(xAnimation, new PropertyPath(TranslateTransform.XProperty));
Storyboard.SetTargetName(yAnimation, "InteractiveTranslateTransform");
Storyboard.SetTargetProperty(yAnimation, new PropertyPath(TranslateTransform.YProperty));
theStoryboard = new Storyboard();
theStoryboard.Children.Add(xAnimation);
theStoryboard.Children.Add(yAnimation);
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)
{
Point clickPoint = Mouse.GetPosition(containerBorder);
// Set the target point so the center of the ellipse
// ends up at the clicked point.
Point targetPoint = new Point();
targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2;
targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2;
// Animate to the target point.
xAnimation.To = targetPoint.X;
yAnimation.To = targetPoint.Y;
theStoryboard.Begin(this, HandoffBehavior.SnapshotAndReplace);
// Change the color of the ellipse.
interactiveEllipse.Fill = 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.
Point clickPoint = Mouse.GetPosition(containerBorder);
// Set the target point so the center of the ellipse
// ends up at the clicked point.
Point targetPoint = new Point();
targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2;
targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2;
// Animate to the target point.
xAnimation.To = targetPoint.X;
yAnimation.To = targetPoint.Y;
theStoryboard.Begin(this, HandoffBehavior.Compose);
// Change the color of the ellipse.
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.
'
'
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.AnimatingWithStoryboards
' Create the demonstration.
Public Class FrameworkElementStoryboardHandoffBehaviorExample
Inherits Page
Private containerBorder As Border
Private interactiveEllipse As Ellipse
Private theStoryboard As Storyboard
Private xAnimation As DoubleAnimation
Private yAnimation As DoubleAnimation
Public Sub New()
WindowTitle = "Interactive Animation Example"
' Create a name scope for the page.
NameScope.SetNameScope(Me, New NameScope())
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()
With interactiveEllipse
.Fill = Brushes.Lime
.Stroke = Brushes.Black
.StrokeThickness = 2.0
.Width = 25
.Height = 25
.HorizontalAlignment = HorizontalAlignment.Left
.VerticalAlignment = VerticalAlignment.Top
End With
Dim interactiveTranslateTransform As New TranslateTransform()
Me.RegisterName("InteractiveTranslateTransform", interactiveTranslateTransform)
interactiveEllipse.RenderTransform = interactiveTranslateTransform
xAnimation = New DoubleAnimation()
xAnimation.Duration = TimeSpan.FromSeconds(4)
yAnimation = xAnimation.Clone()
Storyboard.SetTargetName(xAnimation, "InteractiveTranslateTransform")
Storyboard.SetTargetProperty(xAnimation, New PropertyPath(TranslateTransform.XProperty))
Storyboard.SetTargetName(yAnimation, "InteractiveTranslateTransform")
Storyboard.SetTargetProperty(yAnimation, New PropertyPath(TranslateTransform.YProperty))
theStoryboard = New Storyboard()
theStoryboard.Children.Add(xAnimation)
theStoryboard.Children.Add(yAnimation)
AddHandler containerBorder.MouseLeftButtonDown, AddressOf border_mouseLeftButtonDown
AddHandler containerBorder.MouseRightButtonDown, AddressOf border_mouseRightButtonDown
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)
Dim clickPoint As Point = Mouse.GetPosition(containerBorder)
' Set the target point so the center of the ellipse
' ends up at the clicked point.
Dim targetPoint As New Point()
targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2
targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2
' Animate to the target point.
xAnimation.To = targetPoint.X
yAnimation.To = targetPoint.Y
theStoryboard.Begin(Me, 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)
' Find the point where the use clicked.
Dim clickPoint As Point = Mouse.GetPosition(containerBorder)
' Set the target point so the center of the ellipse
' ends up at the clicked point.
Dim targetPoint As New Point()
targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2
targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2
' Animate to the target point.
xAnimation.To = targetPoint.X
yAnimation.To = targetPoint.Y
theStoryboard.Begin(Me, HandoffBehavior.Compose)
' Change the color of the ellipse.
interactiveEllipse.Fill = Brushes.Orange
End Sub
End Class
End Namespace
備註
若要以互動方式控制這個分鏡腳本,您必須在呼叫用來開始分鏡腳本的互動式方法時使用相同的 containingObject
參數
呼叫此方法時,會為分鏡腳本建立 Clock 物件及其包含的任何時間軸。 這些時鐘會與 containingObject
一起儲存。
開始分鏡腳本會觸發 CurrentStateInvalidated 和 CurrentGlobalSpeedInvalidated 事件。
使用 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。
這主要是物件上具有較長存留期之動畫的問題。 當物件被垃圾收集時,其時鐘也會中斷連線並垃圾收集。
如需時鐘物件的詳細資訊,請參閱 動畫和計時系統概觀。
適用於
Begin(FrameworkElement, FrameworkTemplate, HandoffBehavior)
將與此 Storyboard 相關聯的動畫套用至指定範本內的目標,並加以起始。
public:
void Begin(System::Windows::FrameworkElement ^ containingObject, System::Windows::FrameworkTemplate ^ frameworkTemplate, System::Windows::Media::Animation::HandoffBehavior handoffBehavior);
public void Begin (System.Windows.FrameworkElement containingObject, System.Windows.FrameworkTemplate frameworkTemplate, System.Windows.Media.Animation.HandoffBehavior handoffBehavior);
member this.Begin : System.Windows.FrameworkElement * System.Windows.FrameworkTemplate * System.Windows.Media.Animation.HandoffBehavior -> unit
Public Sub Begin (containingObject As FrameworkElement, frameworkTemplate As FrameworkTemplate, handoffBehavior As HandoffBehavior)
參數
- containingObject
- FrameworkElement
套用指定 frameworkTemplate
的物件。 沒有 TargetName 的動畫會套用至 containingObject
。
- frameworkTemplate
- FrameworkTemplate
要產生動畫效果的範本。
- handoffBehavior
- HandoffBehavior
新動畫應該用來與任何目前動畫互動的行為。
備註
呼叫此方法時,會為分鏡腳本建立 Clock 物件及其包含的任何時間軸。 這些時鐘會與 containingObject
一起儲存。
開始分鏡腳本會觸發 CurrentStateInvalidated 和 CurrentGlobalSpeedInvalidated 事件。
使用 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。
這主要是物件上具有較長存留期之動畫的問題。 當物件被垃圾收集時,其時鐘也會中斷連線並垃圾收集。
如需時鐘物件的詳細資訊,請參閱 動畫和計時系統概觀。
適用於
Begin(FrameworkElement, FrameworkTemplate, Boolean)
將與此 Storyboard 相關聯的動畫套用至指定範本內的目標,並加以起始。
public:
void Begin(System::Windows::FrameworkElement ^ containingObject, System::Windows::FrameworkTemplate ^ frameworkTemplate, bool isControllable);
public void Begin (System.Windows.FrameworkElement containingObject, System.Windows.FrameworkTemplate frameworkTemplate, bool isControllable);
member this.Begin : System.Windows.FrameworkElement * System.Windows.FrameworkTemplate * bool -> unit
Public Sub Begin (containingObject As FrameworkElement, frameworkTemplate As FrameworkTemplate, isControllable As Boolean)
參數
- containingObject
- FrameworkElement
套用指定 frameworkTemplate
的物件。 沒有 TargetName 的動畫會套用至 containingObject
。
- frameworkTemplate
- FrameworkTemplate
要產生動畫效果的範本。
- isControllable
- Boolean
如果分鏡腳本應該以互動方式控制,true
;否則,false
。
備註
如果目標屬性已經以動畫顯示,則會使用 SnapshotAndReplace 交接行為來取代它們。
若要以互動方式控制這個分鏡腳本,您必須在呼叫用來開始分鏡腳本的互動式方法時,指定相同的 containingObject
呼叫此方法時,會為分鏡腳本建立 Clock 物件及其包含的任何時間軸。 這些時鐘會與 containingObject
一起儲存。
開始分鏡腳本會觸發 CurrentStateInvalidated 和 CurrentGlobalSpeedInvalidated 事件。
適用於
Begin(FrameworkContentElement, HandoffBehavior, Boolean)
使用指定的 HandoffBehavior,將與此 Storyboard 相關聯的動畫套用至其目標並加以起始。
public:
void Begin(System::Windows::FrameworkContentElement ^ containingObject, System::Windows::Media::Animation::HandoffBehavior handoffBehavior, bool isControllable);
public void Begin (System.Windows.FrameworkContentElement containingObject, System.Windows.Media.Animation.HandoffBehavior handoffBehavior, bool isControllable);
member this.Begin : System.Windows.FrameworkContentElement * System.Windows.Media.Animation.HandoffBehavior * bool -> unit
Public Sub Begin (containingObject As FrameworkContentElement, handoffBehavior As HandoffBehavior, isControllable As Boolean)
參數
- containingObject
- FrameworkContentElement
物件包含在與這個分鏡腳本動畫目標相同的名稱範圍內。 沒有指定 TargetName 的動畫會套用至 containingObject
。
- handoffBehavior
- HandoffBehavior
新動畫應該用來與任何目前動畫互動的行為。
- isControllable
- Boolean
宣告動畫是否可控制(可暫停)一旦啟動。
範例
下列範例會使用可控制的分鏡腳本,以動畫顯示 TextEffect。 TextEffect 包含在 FrameworkContentElement的名稱範圍內。
/*
This example shows how to control
a storyboard after it has started.
*/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Documents;
namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
{
public class FrameworkContentElementControlStoryboardExample : FlowDocument
{
private Storyboard myStoryboard;
public FrameworkContentElementControlStoryboardExample()
{
// Create a name scope for the document.
NameScope.SetNameScope(this, new NameScope());
this.Background = Brushes.White;
// Create a run of text.
Run theText = new Run(
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit." +
"Ut non lacus. Nullam a ligula id leo adipiscing ornare." +
" Duis mattis. ");
// Create a TextEffect
TextEffect animatedSpecialEffect = new TextEffect();
animatedSpecialEffect.Foreground = Brushes.OrangeRed;
animatedSpecialEffect.PositionStart = 0;
animatedSpecialEffect.PositionCount = 0;
// Assign the TextEffect a name by
// registering it with the page, so that
// it can be targeted by storyboard
// animations
this.RegisterName("animatedSpecialEffect", animatedSpecialEffect);
// Apply the text effect to the run.
theText.TextEffects = new TextEffectCollection();
theText.TextEffects.Add(animatedSpecialEffect);
// Create a paragraph to contain the run.
Paragraph animatedParagraph = new Paragraph(theText);
animatedParagraph.Background = Brushes.LightGray;
animatedParagraph.Padding = new Thickness(20);
this.Blocks.Add(animatedParagraph);
BlockUIContainer controlsContainer = new BlockUIContainer();
//
// Create an animation and a storyboard to animate the
// text effect.
//
Int32Animation countAnimation =
new Int32Animation(0, 127, TimeSpan.FromSeconds(10));
Storyboard.SetTargetName(countAnimation, "animatedSpecialEffect");
Storyboard.SetTargetProperty(countAnimation,
new PropertyPath(TextEffect.PositionCountProperty));
myStoryboard = new Storyboard();
myStoryboard.Children.Add(countAnimation);
//
// Create some buttons to control the storyboard
// and a panel to contain them.
//
StackPanel buttonPanel = new StackPanel();
buttonPanel.Orientation = Orientation.Vertical;
Button beginButton = new Button();
beginButton.Content = "Begin";
beginButton.Click += new RoutedEventHandler(beginButton_Clicked);
buttonPanel.Children.Add(beginButton);
Button pauseButton = new Button();
pauseButton.Content = "Pause";
pauseButton.Click +=new RoutedEventHandler(pauseButton_Clicked);
buttonPanel.Children.Add(pauseButton);
Button resumeButton = new Button();
resumeButton.Content = "Resume";
resumeButton.Click +=new RoutedEventHandler(resumeButton_Clicked);
buttonPanel.Children.Add(resumeButton);
Button skipToFillButton = new Button();
skipToFillButton.Content = "Skip to Fill";
skipToFillButton.Click +=new RoutedEventHandler(skipToFillButton_Clicked);
buttonPanel.Children.Add(skipToFillButton);
Button setSpeedRatioButton = new Button();
setSpeedRatioButton.Content = "Triple Speed";
setSpeedRatioButton.Click +=new RoutedEventHandler(setSpeedRatioButton_Clicked);
buttonPanel.Children.Add(setSpeedRatioButton);
Button stopButton = new Button();
stopButton.Content = "Stop";
stopButton.Click +=new RoutedEventHandler(stopButton_Clicked);
buttonPanel.Children.Add(stopButton);
Button removeButton = new Button();
removeButton.Content = "Remove";
removeButton.Click +=new RoutedEventHandler(removeButton_Clicked);
buttonPanel.Children.Add(removeButton);
controlsContainer.Child = buttonPanel;
this.Blocks.Add(controlsContainer);
}
// Begins the storyboard.
private void beginButton_Clicked(object sender, RoutedEventArgs args)
{
// Specifying "true" as the second Begin parameter
// makes this storyboard controllable.
myStoryboard.Begin(this, true);
}
// Pauses the storyboard.
private void pauseButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Pause(this);
}
// Resumes the storyboard.
private void resumeButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Resume(this);
}
// Advances the storyboard to its fill period.
private void skipToFillButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.SkipToFill(this);
}
// Updates the storyboard's speed.
private void setSpeedRatioButton_Clicked(object sender, RoutedEventArgs args)
{
// Makes the storyboard progress three times as fast as normal.
myStoryboard.SetSpeedRatio(this, 3);
}
// Stops the storyboard.
private void stopButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Stop(this);
}
// Removes the storyboard.
private void removeButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Remove(this);
}
}
}
'
' This example shows how to control
' a storyboard after it has started.
'
'
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports System.Windows.Media.Animation
Imports System.Windows.Documents
Namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
Public Class FrameworkContentElementControlStoryboardExample
Inherits FlowDocument
Private myStoryboard As Storyboard
Public Sub New()
' Create a name scope for the document.
NameScope.SetNameScope(Me, New NameScope())
Me.Background = Brushes.White
' Create a run of text.
Dim theText As New Run("Lorem ipsum dolor sit amet, consectetuer adipiscing elit." & "Ut non lacus. Nullam a ligula id leo adipiscing ornare." & " Duis mattis. ")
' Create a TextEffect
Dim animatedSpecialEffect As New TextEffect()
animatedSpecialEffect.Foreground = Brushes.OrangeRed
animatedSpecialEffect.PositionStart = 0
animatedSpecialEffect.PositionCount = 0
' Assign the TextEffect a name by
' registering it with the page, so that
' it can be targeted by storyboard
' animations
Me.RegisterName("animatedSpecialEffect", animatedSpecialEffect)
' Apply the text effect to the run.
theText.TextEffects = New TextEffectCollection()
theText.TextEffects.Add(animatedSpecialEffect)
' Create a paragraph to contain the run.
Dim animatedParagraph As New Paragraph(theText)
animatedParagraph.Background = Brushes.LightGray
animatedParagraph.Padding = New Thickness(20)
Me.Blocks.Add(animatedParagraph)
Dim controlsContainer As New BlockUIContainer()
'
' Create an animation and a storyboard to animate the
' text effect.
'
Dim countAnimation As New Int32Animation(0, 127, TimeSpan.FromSeconds(10))
Storyboard.SetTargetName(countAnimation, "animatedSpecialEffect")
Storyboard.SetTargetProperty(countAnimation, New PropertyPath(TextEffect.PositionCountProperty))
myStoryboard = New Storyboard()
myStoryboard.Children.Add(countAnimation)
'
' Create some buttons to control the storyboard
' and a panel to contain them.
'
Dim buttonPanel As New StackPanel()
buttonPanel.Orientation = Orientation.Vertical
Dim beginButton As New Button()
beginButton.Content = "Begin"
AddHandler beginButton.Click, AddressOf beginButton_Clicked
buttonPanel.Children.Add(beginButton)
Dim pauseButton As New Button()
pauseButton.Content = "Pause"
AddHandler pauseButton.Click, AddressOf pauseButton_Clicked
buttonPanel.Children.Add(pauseButton)
Dim resumeButton As New Button()
resumeButton.Content = "Resume"
AddHandler resumeButton.Click, AddressOf resumeButton_Clicked
buttonPanel.Children.Add(resumeButton)
Dim skipToFillButton As New Button()
skipToFillButton.Content = "Skip to Fill"
AddHandler skipToFillButton.Click, AddressOf skipToFillButton_Clicked
buttonPanel.Children.Add(skipToFillButton)
Dim setSpeedRatioButton As New Button()
setSpeedRatioButton.Content = "Triple Speed"
AddHandler setSpeedRatioButton.Click, AddressOf setSpeedRatioButton_Clicked
buttonPanel.Children.Add(setSpeedRatioButton)
Dim stopButton As New Button()
stopButton.Content = "Stop"
AddHandler stopButton.Click, AddressOf stopButton_Clicked
buttonPanel.Children.Add(stopButton)
Dim removeButton As New Button()
removeButton.Content = "Remove"
AddHandler removeButton.Click, AddressOf removeButton_Clicked
buttonPanel.Children.Add(removeButton)
controlsContainer.Child = buttonPanel
Me.Blocks.Add(controlsContainer)
End Sub
' Begins the storyboard.
Private Sub beginButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
' Specifying "true" as the second Begin parameter
' makes this storyboard controllable.
myStoryboard.Begin(Me, True)
End Sub
' Pauses the storyboard.
Private Sub pauseButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Pause(Me)
End Sub
' Resumes the storyboard.
Private Sub resumeButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Resume(Me)
End Sub
' Advances the storyboard to its fill period.
Private Sub skipToFillButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.SkipToFill(Me)
End Sub
' Updates the storyboard's speed.
Private Sub setSpeedRatioButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
' Makes the storyboard progress three times as fast as normal.
myStoryboard.SetSpeedRatio(Me, 3)
End Sub
' Stops the storyboard.
Private Sub stopButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Stop(Me)
End Sub
' Removes the storyboard.
Private Sub removeButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Remove(Me)
End Sub
End Class
End Namespace
下一個範例會使用 SnapshotAndReplaceHandoffBehavior,在使用者按兩下滑鼠左鍵時產生動畫效果,並在使用者按兩下滑鼠右鍵時 ComposeHandoffBehavior。
/*
This example shows how to animate
a FrameworkContentElement with a storyboard.
*/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Documents;
using System.Windows.Input;
namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
{
public class FrameworkContentElementStoryboardWithHandoffBehaviorExample :
FlowDocument
{
private Storyboard myStoryboard;
DoubleAnimation xAnimation;
DoubleAnimation yAnimation;
public FrameworkContentElementStoryboardWithHandoffBehaviorExample()
{
// Create a name scope for the document.
NameScope.SetNameScope(this, new NameScope());
this.Background = Brushes.Orange;
// Create a run of text.
Run theText = new Run(
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");
// Create a TextEffect
TextEffect animatedSpecialEffect = new TextEffect();
animatedSpecialEffect.Foreground = Brushes.OrangeRed;
animatedSpecialEffect.PositionStart = 0;
animatedSpecialEffect.PositionCount = 20;
TranslateTransform animatedTransform =
new TranslateTransform();
// Assign the transform a name by
// registering it with the page, so that
// it can be targeted by storyboard
// animations.
this.RegisterName("animatedTransform", animatedTransform);
animatedSpecialEffect.Transform = animatedTransform;
// Apply the text effect to the run.
theText.TextEffects = new TextEffectCollection();
theText.TextEffects.Add(animatedSpecialEffect);
// Create a paragraph to contain the run.
Paragraph animatedParagraph = new Paragraph(theText);
animatedParagraph.Background = Brushes.LightGray;
this.Blocks.Add(animatedParagraph);
//
// Create a storyboard to animate the
// text effect's transform.
//
myStoryboard = new Storyboard();
xAnimation = new DoubleAnimation();
xAnimation.Duration = TimeSpan.FromSeconds(5);
Storyboard.SetTargetName(xAnimation, "animatedTransform");
Storyboard.SetTargetProperty(xAnimation,
new PropertyPath(TranslateTransform.XProperty));
myStoryboard.Children.Add(xAnimation);
yAnimation = new DoubleAnimation();
yAnimation.Duration = TimeSpan.FromSeconds(5);
Storyboard.SetTargetName(yAnimation, "animatedTransform");
Storyboard.SetTargetProperty(yAnimation,
new PropertyPath(TranslateTransform.YProperty));
myStoryboard.Children.Add(yAnimation);
this.MouseLeftButtonDown +=
new MouseButtonEventHandler(document_mouseLeftButtonDown);
this.MouseRightButtonDown +=
new MouseButtonEventHandler(document_mouseRightButtonDown);
}
// When the user left-clicks, use the
// SnapshotAndReplace HandoffBehavior when applying the animation.
private void document_mouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point clickPoint = Mouse.GetPosition(this);
// Animate to the target point.
xAnimation.To = clickPoint.X;
yAnimation.To = clickPoint.Y;
try
{
myStoryboard.Begin(this, HandoffBehavior.SnapshotAndReplace);
}catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
// When the user right-clicks, use the
// Compose HandoffBehavior when applying the animation.
private void document_mouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
Point clickPoint = Mouse.GetPosition(this);
// Animate to the target point.
xAnimation.To = clickPoint.X;
yAnimation.To = clickPoint.Y;
myStoryboard.Begin(this, HandoffBehavior.Compose);
}
}
}
'
' This example shows how to animate
' a FrameworkContentElement with a storyboard.
'
'
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports System.Windows.Media.Animation
Imports System.Windows.Documents
Imports System.Windows.Input
Namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
Public Class FrameworkContentElementStoryboardWithHandoffBehaviorExample
Inherits FlowDocument
Private myStoryboard As Storyboard
Private xAnimation As DoubleAnimation
Private yAnimation As DoubleAnimation
Public Sub New()
' Create a name scope for the document.
NameScope.SetNameScope(Me, New NameScope())
Me.Background = Brushes.Orange
' Create a run of text.
Dim theText As New Run("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
' Create a TextEffect
Dim animatedSpecialEffect As New TextEffect()
animatedSpecialEffect.Foreground = Brushes.OrangeRed
animatedSpecialEffect.PositionStart = 0
animatedSpecialEffect.PositionCount = 20
Dim animatedTransform As New TranslateTransform()
' Assign the transform a name by
' registering it with the page, so that
' it can be targeted by storyboard
' animations.
Me.RegisterName("animatedTransform", animatedTransform)
animatedSpecialEffect.Transform = animatedTransform
' Apply the text effect to the run.
theText.TextEffects = New TextEffectCollection()
theText.TextEffects.Add(animatedSpecialEffect)
' Create a paragraph to contain the run.
Dim animatedParagraph As New Paragraph(theText)
animatedParagraph.Background = Brushes.LightGray
Me.Blocks.Add(animatedParagraph)
'
' Create a storyboard to animate the
' text effect's transform.
'
myStoryboard = New Storyboard()
xAnimation = New DoubleAnimation()
xAnimation.Duration = TimeSpan.FromSeconds(5)
Storyboard.SetTargetName(xAnimation, "animatedTransform")
Storyboard.SetTargetProperty(xAnimation, New PropertyPath(TranslateTransform.XProperty))
myStoryboard.Children.Add(xAnimation)
yAnimation = New DoubleAnimation()
yAnimation.Duration = TimeSpan.FromSeconds(5)
Storyboard.SetTargetName(yAnimation, "animatedTransform")
Storyboard.SetTargetProperty(yAnimation, New PropertyPath(TranslateTransform.YProperty))
myStoryboard.Children.Add(yAnimation)
AddHandler MouseLeftButtonDown, AddressOf document_mouseLeftButtonDown
AddHandler MouseRightButtonDown, AddressOf document_mouseRightButtonDown
End Sub
' When the user left-clicks, use the
' SnapshotAndReplace HandoffBehavior when applying the animation.
Private Sub document_mouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
Dim clickPoint As Point = Mouse.GetPosition(Me)
' Animate to the target point.
xAnimation.To = clickPoint.X
yAnimation.To = clickPoint.Y
Try
myStoryboard.Begin(Me, HandoffBehavior.SnapshotAndReplace)
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub
' When the user right-clicks, use the
' Compose HandoffBehavior when applying the animation.
Private Sub document_mouseRightButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
Dim clickPoint As Point = Mouse.GetPosition(Me)
' Animate to the target point.
xAnimation.To = clickPoint.X
yAnimation.To = clickPoint.Y
myStoryboard.Begin(Me, HandoffBehavior.Compose)
End Sub
End Class
End Namespace
備註
如果目標屬性已經以動畫顯示,則會使用指定的交接行為來取代它們。
若要以互動方式控制這個分鏡腳本,您必須在呼叫用來開始分鏡腳本的互動式方法時,指定相同的 containingObject
呼叫此方法時,會為分鏡腳本建立 Clock 物件及其包含的任何時間軸。 這些時鐘會與 containingObject
一起儲存。
開始分鏡腳本會觸發 CurrentStateInvalidated 和 CurrentGlobalSpeedInvalidated 事件。
使用 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。
這主要是物件上具有較長存留期之動畫的問題。 當物件被垃圾收集時,其時鐘也會中斷連線並垃圾收集。
如需時鐘物件的詳細資訊,請參閱 動畫和計時系統概觀。
適用於
Begin(FrameworkElement, FrameworkTemplate)
將與此 Storyboard 相關聯的動畫套用至指定範本內的目標,並加以起始。
public:
void Begin(System::Windows::FrameworkElement ^ containingObject, System::Windows::FrameworkTemplate ^ frameworkTemplate);
public void Begin (System.Windows.FrameworkElement containingObject, System.Windows.FrameworkTemplate frameworkTemplate);
member this.Begin : System.Windows.FrameworkElement * System.Windows.FrameworkTemplate -> unit
Public Sub Begin (containingObject As FrameworkElement, frameworkTemplate As FrameworkTemplate)
參數
- containingObject
- FrameworkElement
套用指定 frameworkTemplate
的物件。 沒有 TargetName 的動畫會套用至 containingObject
。
- frameworkTemplate
- FrameworkTemplate
要產生動畫效果的範本。
備註
如果目標屬性已經以動畫顯示,則會使用 SnapshotAndReplace 交接行為來取代它們。
呼叫此方法時,會為分鏡腳本建立 Clock 物件及其包含的任何時間軸。 這些時鐘會與 containingObject
一起儲存。
開始分鏡腳本會觸發 CurrentStateInvalidated 和 CurrentGlobalSpeedInvalidated 事件。
適用於
Begin(FrameworkElement, HandoffBehavior)
使用指定的 HandoffBehavior,將與此 Storyboard 相關聯的動畫套用至其目標並加以起始。
public:
void Begin(System::Windows::FrameworkElement ^ containingObject, System::Windows::Media::Animation::HandoffBehavior handoffBehavior);
public void Begin (System.Windows.FrameworkElement containingObject, System.Windows.Media.Animation.HandoffBehavior handoffBehavior);
member this.Begin : System.Windows.FrameworkElement * System.Windows.Media.Animation.HandoffBehavior -> unit
Public Sub Begin (containingObject As FrameworkElement, handoffBehavior As HandoffBehavior)
參數
- containingObject
- FrameworkElement
物件包含在與這個分鏡腳本動畫目標相同的名稱範圍內。 沒有指定 TargetName 的動畫會套用至 containingObject
。
- handoffBehavior
- HandoffBehavior
新動畫應該用來與任何目前動畫互動的行為。
範例
下列範例會使用 SnapshotAndReplaceHandoffBehavior,在使用者按兩下滑鼠左鍵時產生動畫效果,並在使用者按兩下滑鼠右鍵時 ComposeHandoffBehavior。
/*
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.AnimatingWithStoryboards
{
// Create the demonstration.
public class FrameworkElementStoryboardHandoffBehaviorExample : Page {
private Border containerBorder;
private Ellipse interactiveEllipse;
private Storyboard theStoryboard;
private DoubleAnimation xAnimation;
private DoubleAnimation yAnimation;
public FrameworkElementStoryboardHandoffBehaviorExample()
{
WindowTitle = "Interactive Animation Example";
// Create a name scope for the page.
NameScope.SetNameScope(this, new NameScope());
DockPanel myPanel = 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;
TranslateTransform interactiveTranslateTransform = new TranslateTransform();
this.RegisterName("InteractiveTranslateTransform", interactiveTranslateTransform);
interactiveEllipse.RenderTransform =
interactiveTranslateTransform;
xAnimation = new DoubleAnimation();
xAnimation.Duration = TimeSpan.FromSeconds(4);
yAnimation = xAnimation.Clone();
Storyboard.SetTargetName(xAnimation, "InteractiveTranslateTransform");
Storyboard.SetTargetProperty(xAnimation, new PropertyPath(TranslateTransform.XProperty));
Storyboard.SetTargetName(yAnimation, "InteractiveTranslateTransform");
Storyboard.SetTargetProperty(yAnimation, new PropertyPath(TranslateTransform.YProperty));
theStoryboard = new Storyboard();
theStoryboard.Children.Add(xAnimation);
theStoryboard.Children.Add(yAnimation);
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)
{
Point clickPoint = Mouse.GetPosition(containerBorder);
// Set the target point so the center of the ellipse
// ends up at the clicked point.
Point targetPoint = new Point();
targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2;
targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2;
// Animate to the target point.
xAnimation.To = targetPoint.X;
yAnimation.To = targetPoint.Y;
theStoryboard.Begin(this, HandoffBehavior.SnapshotAndReplace);
// Change the color of the ellipse.
interactiveEllipse.Fill = 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.
Point clickPoint = Mouse.GetPosition(containerBorder);
// Set the target point so the center of the ellipse
// ends up at the clicked point.
Point targetPoint = new Point();
targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2;
targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2;
// Animate to the target point.
xAnimation.To = targetPoint.X;
yAnimation.To = targetPoint.Y;
theStoryboard.Begin(this, HandoffBehavior.Compose);
// Change the color of the ellipse.
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.
'
'
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.AnimatingWithStoryboards
' Create the demonstration.
Public Class FrameworkElementStoryboardHandoffBehaviorExample
Inherits Page
Private containerBorder As Border
Private interactiveEllipse As Ellipse
Private theStoryboard As Storyboard
Private xAnimation As DoubleAnimation
Private yAnimation As DoubleAnimation
Public Sub New()
WindowTitle = "Interactive Animation Example"
' Create a name scope for the page.
NameScope.SetNameScope(Me, New NameScope())
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()
With interactiveEllipse
.Fill = Brushes.Lime
.Stroke = Brushes.Black
.StrokeThickness = 2.0
.Width = 25
.Height = 25
.HorizontalAlignment = HorizontalAlignment.Left
.VerticalAlignment = VerticalAlignment.Top
End With
Dim interactiveTranslateTransform As New TranslateTransform()
Me.RegisterName("InteractiveTranslateTransform", interactiveTranslateTransform)
interactiveEllipse.RenderTransform = interactiveTranslateTransform
xAnimation = New DoubleAnimation()
xAnimation.Duration = TimeSpan.FromSeconds(4)
yAnimation = xAnimation.Clone()
Storyboard.SetTargetName(xAnimation, "InteractiveTranslateTransform")
Storyboard.SetTargetProperty(xAnimation, New PropertyPath(TranslateTransform.XProperty))
Storyboard.SetTargetName(yAnimation, "InteractiveTranslateTransform")
Storyboard.SetTargetProperty(yAnimation, New PropertyPath(TranslateTransform.YProperty))
theStoryboard = New Storyboard()
theStoryboard.Children.Add(xAnimation)
theStoryboard.Children.Add(yAnimation)
AddHandler containerBorder.MouseLeftButtonDown, AddressOf border_mouseLeftButtonDown
AddHandler containerBorder.MouseRightButtonDown, AddressOf border_mouseRightButtonDown
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)
Dim clickPoint As Point = Mouse.GetPosition(containerBorder)
' Set the target point so the center of the ellipse
' ends up at the clicked point.
Dim targetPoint As New Point()
targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2
targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2
' Animate to the target point.
xAnimation.To = targetPoint.X
yAnimation.To = targetPoint.Y
theStoryboard.Begin(Me, 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)
' Find the point where the use clicked.
Dim clickPoint As Point = Mouse.GetPosition(containerBorder)
' Set the target point so the center of the ellipse
' ends up at the clicked point.
Dim targetPoint As New Point()
targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2
targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2
' Animate to the target point.
xAnimation.To = targetPoint.X
yAnimation.To = targetPoint.Y
theStoryboard.Begin(Me, HandoffBehavior.Compose)
' Change the color of the ellipse.
interactiveEllipse.Fill = Brushes.Orange
End Sub
End Class
End Namespace
適用於
Begin(FrameworkContentElement, HandoffBehavior)
使用指定的 HandoffBehavior,將與此 Storyboard 相關聯的動畫套用至其目標並加以起始。
public:
void Begin(System::Windows::FrameworkContentElement ^ containingObject, System::Windows::Media::Animation::HandoffBehavior handoffBehavior);
public void Begin (System.Windows.FrameworkContentElement containingObject, System.Windows.Media.Animation.HandoffBehavior handoffBehavior);
member this.Begin : System.Windows.FrameworkContentElement * System.Windows.Media.Animation.HandoffBehavior -> unit
Public Sub Begin (containingObject As FrameworkContentElement, handoffBehavior As HandoffBehavior)
參數
- containingObject
- FrameworkContentElement
物件包含在與這個分鏡腳本動畫目標相同的名稱範圍內。 沒有 TargetName 的動畫會套用至 containingObject
。
- handoffBehavior
- HandoffBehavior
新動畫應該用來與任何目前動畫互動的行為。
範例
下列範例會使用 SnapshotAndReplaceHandoffBehavior,在使用者按兩下滑鼠左鍵時產生動畫效果,並在使用者按兩下滑鼠右鍵時 ComposeHandoffBehavior。
/*
This example shows how to animate
a FrameworkContentElement with a storyboard.
*/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Documents;
using System.Windows.Input;
namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
{
public class FrameworkContentElementStoryboardWithHandoffBehaviorExample :
FlowDocument
{
private Storyboard myStoryboard;
DoubleAnimation xAnimation;
DoubleAnimation yAnimation;
public FrameworkContentElementStoryboardWithHandoffBehaviorExample()
{
// Create a name scope for the document.
NameScope.SetNameScope(this, new NameScope());
this.Background = Brushes.Orange;
// Create a run of text.
Run theText = new Run(
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");
// Create a TextEffect
TextEffect animatedSpecialEffect = new TextEffect();
animatedSpecialEffect.Foreground = Brushes.OrangeRed;
animatedSpecialEffect.PositionStart = 0;
animatedSpecialEffect.PositionCount = 20;
TranslateTransform animatedTransform =
new TranslateTransform();
// Assign the transform a name by
// registering it with the page, so that
// it can be targeted by storyboard
// animations.
this.RegisterName("animatedTransform", animatedTransform);
animatedSpecialEffect.Transform = animatedTransform;
// Apply the text effect to the run.
theText.TextEffects = new TextEffectCollection();
theText.TextEffects.Add(animatedSpecialEffect);
// Create a paragraph to contain the run.
Paragraph animatedParagraph = new Paragraph(theText);
animatedParagraph.Background = Brushes.LightGray;
this.Blocks.Add(animatedParagraph);
//
// Create a storyboard to animate the
// text effect's transform.
//
myStoryboard = new Storyboard();
xAnimation = new DoubleAnimation();
xAnimation.Duration = TimeSpan.FromSeconds(5);
Storyboard.SetTargetName(xAnimation, "animatedTransform");
Storyboard.SetTargetProperty(xAnimation,
new PropertyPath(TranslateTransform.XProperty));
myStoryboard.Children.Add(xAnimation);
yAnimation = new DoubleAnimation();
yAnimation.Duration = TimeSpan.FromSeconds(5);
Storyboard.SetTargetName(yAnimation, "animatedTransform");
Storyboard.SetTargetProperty(yAnimation,
new PropertyPath(TranslateTransform.YProperty));
myStoryboard.Children.Add(yAnimation);
this.MouseLeftButtonDown +=
new MouseButtonEventHandler(document_mouseLeftButtonDown);
this.MouseRightButtonDown +=
new MouseButtonEventHandler(document_mouseRightButtonDown);
}
// When the user left-clicks, use the
// SnapshotAndReplace HandoffBehavior when applying the animation.
private void document_mouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point clickPoint = Mouse.GetPosition(this);
// Animate to the target point.
xAnimation.To = clickPoint.X;
yAnimation.To = clickPoint.Y;
try
{
myStoryboard.Begin(this, HandoffBehavior.SnapshotAndReplace);
}catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
// When the user right-clicks, use the
// Compose HandoffBehavior when applying the animation.
private void document_mouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
Point clickPoint = Mouse.GetPosition(this);
// Animate to the target point.
xAnimation.To = clickPoint.X;
yAnimation.To = clickPoint.Y;
myStoryboard.Begin(this, HandoffBehavior.Compose);
}
}
}
'
' This example shows how to animate
' a FrameworkContentElement with a storyboard.
'
'
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports System.Windows.Media.Animation
Imports System.Windows.Documents
Imports System.Windows.Input
Namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
Public Class FrameworkContentElementStoryboardWithHandoffBehaviorExample
Inherits FlowDocument
Private myStoryboard As Storyboard
Private xAnimation As DoubleAnimation
Private yAnimation As DoubleAnimation
Public Sub New()
' Create a name scope for the document.
NameScope.SetNameScope(Me, New NameScope())
Me.Background = Brushes.Orange
' Create a run of text.
Dim theText As New Run("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
' Create a TextEffect
Dim animatedSpecialEffect As New TextEffect()
animatedSpecialEffect.Foreground = Brushes.OrangeRed
animatedSpecialEffect.PositionStart = 0
animatedSpecialEffect.PositionCount = 20
Dim animatedTransform As New TranslateTransform()
' Assign the transform a name by
' registering it with the page, so that
' it can be targeted by storyboard
' animations.
Me.RegisterName("animatedTransform", animatedTransform)
animatedSpecialEffect.Transform = animatedTransform
' Apply the text effect to the run.
theText.TextEffects = New TextEffectCollection()
theText.TextEffects.Add(animatedSpecialEffect)
' Create a paragraph to contain the run.
Dim animatedParagraph As New Paragraph(theText)
animatedParagraph.Background = Brushes.LightGray
Me.Blocks.Add(animatedParagraph)
'
' Create a storyboard to animate the
' text effect's transform.
'
myStoryboard = New Storyboard()
xAnimation = New DoubleAnimation()
xAnimation.Duration = TimeSpan.FromSeconds(5)
Storyboard.SetTargetName(xAnimation, "animatedTransform")
Storyboard.SetTargetProperty(xAnimation, New PropertyPath(TranslateTransform.XProperty))
myStoryboard.Children.Add(xAnimation)
yAnimation = New DoubleAnimation()
yAnimation.Duration = TimeSpan.FromSeconds(5)
Storyboard.SetTargetName(yAnimation, "animatedTransform")
Storyboard.SetTargetProperty(yAnimation, New PropertyPath(TranslateTransform.YProperty))
myStoryboard.Children.Add(yAnimation)
AddHandler MouseLeftButtonDown, AddressOf document_mouseLeftButtonDown
AddHandler MouseRightButtonDown, AddressOf document_mouseRightButtonDown
End Sub
' When the user left-clicks, use the
' SnapshotAndReplace HandoffBehavior when applying the animation.
Private Sub document_mouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
Dim clickPoint As Point = Mouse.GetPosition(Me)
' Animate to the target point.
xAnimation.To = clickPoint.X
yAnimation.To = clickPoint.Y
Try
myStoryboard.Begin(Me, HandoffBehavior.SnapshotAndReplace)
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub
' When the user right-clicks, use the
' Compose HandoffBehavior when applying the animation.
Private Sub document_mouseRightButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
Dim clickPoint As Point = Mouse.GetPosition(Me)
' Animate to the target point.
xAnimation.To = clickPoint.X
yAnimation.To = clickPoint.Y
myStoryboard.Begin(Me, HandoffBehavior.Compose)
End Sub
End Class
End Namespace
備註
當您使用 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。
這主要是物件上具有較長存留期之動畫的問題。 當物件被垃圾收集時,其時鐘也會中斷連線並垃圾收集。
如需時鐘物件的詳細資訊,請參閱 動畫和計時系統概觀。
適用於
Begin(FrameworkContentElement, Boolean)
將與此 Storyboard 相關聯的動畫套用至其目標並加以起始。
public:
void Begin(System::Windows::FrameworkContentElement ^ containingObject, bool isControllable);
public void Begin (System.Windows.FrameworkContentElement containingObject, bool isControllable);
member this.Begin : System.Windows.FrameworkContentElement * bool -> unit
Public Sub Begin (containingObject As FrameworkContentElement, isControllable As Boolean)
參數
- containingObject
- FrameworkContentElement
物件包含在與這個分鏡腳本動畫目標相同的名稱範圍內。 沒有 TargetName 的動畫會套用至 containingObject
。
- isControllable
- Boolean
如果分鏡腳本應該以互動方式控制,true
;否則,false
。
範例
下列範例會使用可控制的分鏡腳本,以動畫顯示 TextEffect。 TextEffect 包含在 FrameworkContentElement的名稱範圍內。
/*
This example shows how to control
a storyboard after it has started.
*/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Documents;
namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
{
public class FrameworkContentElementControlStoryboardExample : FlowDocument
{
private Storyboard myStoryboard;
public FrameworkContentElementControlStoryboardExample()
{
// Create a name scope for the document.
NameScope.SetNameScope(this, new NameScope());
this.Background = Brushes.White;
// Create a run of text.
Run theText = new Run(
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit." +
"Ut non lacus. Nullam a ligula id leo adipiscing ornare." +
" Duis mattis. ");
// Create a TextEffect
TextEffect animatedSpecialEffect = new TextEffect();
animatedSpecialEffect.Foreground = Brushes.OrangeRed;
animatedSpecialEffect.PositionStart = 0;
animatedSpecialEffect.PositionCount = 0;
// Assign the TextEffect a name by
// registering it with the page, so that
// it can be targeted by storyboard
// animations
this.RegisterName("animatedSpecialEffect", animatedSpecialEffect);
// Apply the text effect to the run.
theText.TextEffects = new TextEffectCollection();
theText.TextEffects.Add(animatedSpecialEffect);
// Create a paragraph to contain the run.
Paragraph animatedParagraph = new Paragraph(theText);
animatedParagraph.Background = Brushes.LightGray;
animatedParagraph.Padding = new Thickness(20);
this.Blocks.Add(animatedParagraph);
BlockUIContainer controlsContainer = new BlockUIContainer();
//
// Create an animation and a storyboard to animate the
// text effect.
//
Int32Animation countAnimation =
new Int32Animation(0, 127, TimeSpan.FromSeconds(10));
Storyboard.SetTargetName(countAnimation, "animatedSpecialEffect");
Storyboard.SetTargetProperty(countAnimation,
new PropertyPath(TextEffect.PositionCountProperty));
myStoryboard = new Storyboard();
myStoryboard.Children.Add(countAnimation);
//
// Create some buttons to control the storyboard
// and a panel to contain them.
//
StackPanel buttonPanel = new StackPanel();
buttonPanel.Orientation = Orientation.Vertical;
Button beginButton = new Button();
beginButton.Content = "Begin";
beginButton.Click += new RoutedEventHandler(beginButton_Clicked);
buttonPanel.Children.Add(beginButton);
Button pauseButton = new Button();
pauseButton.Content = "Pause";
pauseButton.Click +=new RoutedEventHandler(pauseButton_Clicked);
buttonPanel.Children.Add(pauseButton);
Button resumeButton = new Button();
resumeButton.Content = "Resume";
resumeButton.Click +=new RoutedEventHandler(resumeButton_Clicked);
buttonPanel.Children.Add(resumeButton);
Button skipToFillButton = new Button();
skipToFillButton.Content = "Skip to Fill";
skipToFillButton.Click +=new RoutedEventHandler(skipToFillButton_Clicked);
buttonPanel.Children.Add(skipToFillButton);
Button setSpeedRatioButton = new Button();
setSpeedRatioButton.Content = "Triple Speed";
setSpeedRatioButton.Click +=new RoutedEventHandler(setSpeedRatioButton_Clicked);
buttonPanel.Children.Add(setSpeedRatioButton);
Button stopButton = new Button();
stopButton.Content = "Stop";
stopButton.Click +=new RoutedEventHandler(stopButton_Clicked);
buttonPanel.Children.Add(stopButton);
Button removeButton = new Button();
removeButton.Content = "Remove";
removeButton.Click +=new RoutedEventHandler(removeButton_Clicked);
buttonPanel.Children.Add(removeButton);
controlsContainer.Child = buttonPanel;
this.Blocks.Add(controlsContainer);
}
// Begins the storyboard.
private void beginButton_Clicked(object sender, RoutedEventArgs args)
{
// Specifying "true" as the second Begin parameter
// makes this storyboard controllable.
myStoryboard.Begin(this, true);
}
// Pauses the storyboard.
private void pauseButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Pause(this);
}
// Resumes the storyboard.
private void resumeButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Resume(this);
}
// Advances the storyboard to its fill period.
private void skipToFillButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.SkipToFill(this);
}
// Updates the storyboard's speed.
private void setSpeedRatioButton_Clicked(object sender, RoutedEventArgs args)
{
// Makes the storyboard progress three times as fast as normal.
myStoryboard.SetSpeedRatio(this, 3);
}
// Stops the storyboard.
private void stopButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Stop(this);
}
// Removes the storyboard.
private void removeButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Remove(this);
}
}
}
'
' This example shows how to control
' a storyboard after it has started.
'
'
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports System.Windows.Media.Animation
Imports System.Windows.Documents
Namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
Public Class FrameworkContentElementControlStoryboardExample
Inherits FlowDocument
Private myStoryboard As Storyboard
Public Sub New()
' Create a name scope for the document.
NameScope.SetNameScope(Me, New NameScope())
Me.Background = Brushes.White
' Create a run of text.
Dim theText As New Run("Lorem ipsum dolor sit amet, consectetuer adipiscing elit." & "Ut non lacus. Nullam a ligula id leo adipiscing ornare." & " Duis mattis. ")
' Create a TextEffect
Dim animatedSpecialEffect As New TextEffect()
animatedSpecialEffect.Foreground = Brushes.OrangeRed
animatedSpecialEffect.PositionStart = 0
animatedSpecialEffect.PositionCount = 0
' Assign the TextEffect a name by
' registering it with the page, so that
' it can be targeted by storyboard
' animations
Me.RegisterName("animatedSpecialEffect", animatedSpecialEffect)
' Apply the text effect to the run.
theText.TextEffects = New TextEffectCollection()
theText.TextEffects.Add(animatedSpecialEffect)
' Create a paragraph to contain the run.
Dim animatedParagraph As New Paragraph(theText)
animatedParagraph.Background = Brushes.LightGray
animatedParagraph.Padding = New Thickness(20)
Me.Blocks.Add(animatedParagraph)
Dim controlsContainer As New BlockUIContainer()
'
' Create an animation and a storyboard to animate the
' text effect.
'
Dim countAnimation As New Int32Animation(0, 127, TimeSpan.FromSeconds(10))
Storyboard.SetTargetName(countAnimation, "animatedSpecialEffect")
Storyboard.SetTargetProperty(countAnimation, New PropertyPath(TextEffect.PositionCountProperty))
myStoryboard = New Storyboard()
myStoryboard.Children.Add(countAnimation)
'
' Create some buttons to control the storyboard
' and a panel to contain them.
'
Dim buttonPanel As New StackPanel()
buttonPanel.Orientation = Orientation.Vertical
Dim beginButton As New Button()
beginButton.Content = "Begin"
AddHandler beginButton.Click, AddressOf beginButton_Clicked
buttonPanel.Children.Add(beginButton)
Dim pauseButton As New Button()
pauseButton.Content = "Pause"
AddHandler pauseButton.Click, AddressOf pauseButton_Clicked
buttonPanel.Children.Add(pauseButton)
Dim resumeButton As New Button()
resumeButton.Content = "Resume"
AddHandler resumeButton.Click, AddressOf resumeButton_Clicked
buttonPanel.Children.Add(resumeButton)
Dim skipToFillButton As New Button()
skipToFillButton.Content = "Skip to Fill"
AddHandler skipToFillButton.Click, AddressOf skipToFillButton_Clicked
buttonPanel.Children.Add(skipToFillButton)
Dim setSpeedRatioButton As New Button()
setSpeedRatioButton.Content = "Triple Speed"
AddHandler setSpeedRatioButton.Click, AddressOf setSpeedRatioButton_Clicked
buttonPanel.Children.Add(setSpeedRatioButton)
Dim stopButton As New Button()
stopButton.Content = "Stop"
AddHandler stopButton.Click, AddressOf stopButton_Clicked
buttonPanel.Children.Add(stopButton)
Dim removeButton As New Button()
removeButton.Content = "Remove"
AddHandler removeButton.Click, AddressOf removeButton_Clicked
buttonPanel.Children.Add(removeButton)
controlsContainer.Child = buttonPanel
Me.Blocks.Add(controlsContainer)
End Sub
' Begins the storyboard.
Private Sub beginButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
' Specifying "true" as the second Begin parameter
' makes this storyboard controllable.
myStoryboard.Begin(Me, True)
End Sub
' Pauses the storyboard.
Private Sub pauseButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Pause(Me)
End Sub
' Resumes the storyboard.
Private Sub resumeButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Resume(Me)
End Sub
' Advances the storyboard to its fill period.
Private Sub skipToFillButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.SkipToFill(Me)
End Sub
' Updates the storyboard's speed.
Private Sub setSpeedRatioButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
' Makes the storyboard progress three times as fast as normal.
myStoryboard.SetSpeedRatio(Me, 3)
End Sub
' Stops the storyboard.
Private Sub stopButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Stop(Me)
End Sub
' Removes the storyboard.
Private Sub removeButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Remove(Me)
End Sub
End Class
End Namespace
備註
如果目標屬性已經以動畫顯示,則會使用 SnapshotAndReplace 交接行為來取代它們。
若要以互動方式控制這個分鏡腳本,您必須在呼叫用來開始分鏡腳本的互動式方法時,指定相同的 containingObject
呼叫此方法時,會為分鏡腳本建立 Clock 物件及其包含的任何時間軸。 這些時鐘會與 containingObject
一起儲存。
開始分鏡腳本會觸發 CurrentStateInvalidated 和 CurrentGlobalSpeedInvalidated 事件。
適用於
Begin(FrameworkElement)
將與此 Storyboard 相關聯的動畫套用至其目標並加以起始。
public:
void Begin(System::Windows::FrameworkElement ^ containingObject);
public void Begin (System.Windows.FrameworkElement containingObject);
member this.Begin : System.Windows.FrameworkElement -> unit
Public Sub Begin (containingObject As FrameworkElement)
參數
- containingObject
- FrameworkElement
物件包含在與這個分鏡腳本動畫目標相同的名稱範圍內。 沒有 TargetName 的動畫會套用至 containingObject
。
備註
如果目標屬性已經以動畫顯示,則會使用 SnapshotAndReplace 交接行為來取代它們。
以這個方法啟動的分鏡腳本無法在啟動之後暫停、繼續或以互動方式控制。 若要讓分鏡腳本成為可控制,請使用 Begin(FrameworkElement, Boolean) 或 Begin(FrameworkElement, HandoffBehavior, Boolean) 方法。
開始分鏡腳本會觸發 CurrentStateInvalidated 和 CurrentGlobalSpeedInvalidated 事件。
適用於
Begin(FrameworkContentElement)
將與此 Storyboard 相關聯的動畫套用至其目標並加以起始。
public:
void Begin(System::Windows::FrameworkContentElement ^ containingObject);
public void Begin (System.Windows.FrameworkContentElement containingObject);
member this.Begin : System.Windows.FrameworkContentElement -> unit
Public Sub Begin (containingObject As FrameworkContentElement)
參數
- containingObject
- FrameworkContentElement
物件包含在與這個分鏡腳本動畫目標相同的名稱範圍內。 沒有 TargetName 的動畫會套用至 containingObject
。
範例
下列範例會使用分鏡腳本建立 TextEffect動畫。 TextEffect 包含在 FrameworkContentElement的名稱範圍內。
/*
This example shows how to animate
a FrameworkContentElement with a storyboard.
*/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Documents;
namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
{
public class FrameworkContentElementStoryboardExample : FlowDocument
{
private Storyboard myStoryboard;
public FrameworkContentElementStoryboardExample()
{
// Create a name scope for the document.
NameScope.SetNameScope(this, new NameScope());
this.Background = Brushes.White;
// Create a run of text.
Run theText = new Run(
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit." +
"Ut non lacus. Nullam a ligula id leo adipiscing ornare." +
" Duis mattis. ");
// Create a TextEffect
TextEffect animatedSpecialEffect = new TextEffect();
animatedSpecialEffect.Foreground = Brushes.OrangeRed;
animatedSpecialEffect.PositionStart = 0;
animatedSpecialEffect.PositionCount = 0;
// Assign the TextEffect a name by
// registering it with the page, so that
// it can be targeted by storyboard
// animations
this.RegisterName("animatedSpecialEffect", animatedSpecialEffect);
// Apply the text effect to the run.
theText.TextEffects = new TextEffectCollection();
theText.TextEffects.Add(animatedSpecialEffect);
// Create a paragraph to contain the run.
Paragraph animatedParagraph = new Paragraph(theText);
animatedParagraph.Background = Brushes.LightGray;
animatedParagraph.Padding = new Thickness(20);
this.Blocks.Add(animatedParagraph);
BlockUIContainer controlsContainer = new BlockUIContainer();
//
// Create an animation and a storyboard to animate the
// text effect.
//
Int32Animation countAnimation =
new Int32Animation(0, 127, TimeSpan.FromSeconds(10));
Storyboard.SetTargetName(countAnimation, "animatedSpecialEffect");
Storyboard.SetTargetProperty(countAnimation,
new PropertyPath(TextEffect.PositionCountProperty));
myStoryboard = new Storyboard();
myStoryboard.Children.Add(countAnimation);
//
// Create a button to start the storyboard.
//
Button beginButton = new Button();
beginButton.Content = "Begin";
beginButton.Click += new RoutedEventHandler(beginButton_Clicked);
controlsContainer.Child = beginButton;
this.Blocks.Add(controlsContainer);
}
// Begins the storyboard.
private void beginButton_Clicked(object sender, RoutedEventArgs args)
{
myStoryboard.Begin(this);
}
}
}
'
' This example shows how to animate
' a FrameworkContentElement with a storyboard.
'
'
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports System.Windows.Media.Animation
Imports System.Windows.Documents
Namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
Public Class FrameworkContentElementStoryboardExample
Inherits FlowDocument
Private myStoryboard As Storyboard
Public Sub New()
' Create a name scope for the document.
NameScope.SetNameScope(Me, New NameScope())
Me.Background = Brushes.White
' Create a run of text.
Dim theText As New Run("Lorem ipsum dolor sit amet, consectetuer adipiscing elit." & "Ut non lacus. Nullam a ligula id leo adipiscing ornare." & " Duis mattis. ")
' Create a TextEffect
Dim animatedSpecialEffect As New TextEffect()
animatedSpecialEffect.Foreground = Brushes.OrangeRed
animatedSpecialEffect.PositionStart = 0
animatedSpecialEffect.PositionCount = 0
' Assign the TextEffect a name by
' registering it with the page, so that
' it can be targeted by storyboard
' animations
Me.RegisterName("animatedSpecialEffect", animatedSpecialEffect)
' Apply the text effect to the run.
theText.TextEffects = New TextEffectCollection()
theText.TextEffects.Add(animatedSpecialEffect)
' Create a paragraph to contain the run.
Dim animatedParagraph As New Paragraph(theText)
animatedParagraph.Background = Brushes.LightGray
animatedParagraph.Padding = New Thickness(20)
Me.Blocks.Add(animatedParagraph)
Dim controlsContainer As New BlockUIContainer()
'
' Create an animation and a storyboard to animate the
' text effect.
'
Dim countAnimation As New Int32Animation(0, 127, TimeSpan.FromSeconds(10))
Storyboard.SetTargetName(countAnimation, "animatedSpecialEffect")
Storyboard.SetTargetProperty(countAnimation, New PropertyPath(TextEffect.PositionCountProperty))
myStoryboard = New Storyboard()
myStoryboard.Children.Add(countAnimation)
'
' Create a button to start the storyboard.
'
Dim beginButton As New Button()
beginButton.Content = "Begin"
AddHandler beginButton.Click, AddressOf beginButton_Clicked
controlsContainer.Child = beginButton
Me.Blocks.Add(controlsContainer)
End Sub
' Begins the storyboard.
Private Sub beginButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)
myStoryboard.Begin(Me)
End Sub
End Class
End Namespace
備註
如果目標屬性已經以動畫顯示,則會使用 SnapshotAndReplace 交接行為來取代它們。
以這個方法啟動的分鏡腳本無法在啟動之後暫停、繼續或以互動方式控制。 若要讓分鏡腳本成為可控制,請使用 Begin 或 Begin 方法。
開始分鏡腳本會觸發 CurrentStateInvalidated 和 CurrentGlobalSpeedInvalidated 事件。
適用於
Begin()
將與此 Storyboard 相關聯的動畫套用至其目標並加以起始。
public:
void Begin();
public void Begin ();
member this.Begin : unit -> unit
Public Sub Begin ()
適用於
Begin(FrameworkElement, Boolean)
將與此 Storyboard 相關聯的動畫套用至其目標並加以起始。
public:
void Begin(System::Windows::FrameworkElement ^ containingObject, bool isControllable);
public void Begin (System.Windows.FrameworkElement containingObject, bool isControllable);
member this.Begin : System.Windows.FrameworkElement * bool -> unit
Public Sub Begin (containingObject As FrameworkElement, isControllable As Boolean)
參數
- containingObject
- FrameworkElement
物件包含在與這個分鏡腳本動畫目標相同的名稱範圍內。 沒有 TargetName 的動畫會套用至 containingObject
。
- isControllable
- Boolean
如果分鏡腳本應該以互動方式控制,true
;否則,false
。
備註
如果目標屬性已經以動畫顯示,則會使用 SnapshotAndReplace 交接行為來取代它們。
若要以互動方式控制這個分鏡腳本,您必須在呼叫用來開始分鏡腳本的互動式方法時使用相同的 containingObject
參數
呼叫此方法時,會為分鏡腳本建立 Clock 物件及其包含的任何時間軸。 這些時鐘會與 containingObject
一起儲存。
開始分鏡腳本會觸發 CurrentStateInvalidated 和 CurrentGlobalSpeedInvalidated 事件。