語言

Storyboard 類別

定義

它能以時間軸控制動畫,並提供子動畫的物件與屬性目標資訊。

public ref class Storyboard sealed : Timeline
/// [Microsoft.UI.Xaml.Markup.ContentProperty(Name="Children")]
/// [Windows.Foundation.Metadata.Activatable(65536, "Microsoft.UI.Xaml.WinUIContract")]
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class Storyboard final : Timeline
[Microsoft.UI.Xaml.Markup.ContentProperty(Name="Children")]
[Windows.Foundation.Metadata.Activatable(65536, "Microsoft.UI.Xaml.WinUIContract")]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class Storyboard : Timeline
Public NotInheritable Class Storyboard
Inherits Timeline
<Storyboard ...>
  oneOrMoreChildTimelines
</Storyboard>
繼承
Object Platform::Object IInspectable DependencyObject Timeline Storyboard
屬性

範例

以下範例展示了如何使用 開始、 停止、 暫停和 繼續 方法來控制分鏡(動畫)的播放。 一組按鈕讓使用者可以呼叫這些方法。

<StackPanel x:Name="LayoutRoot" >
    <StackPanel.Resources>
        <Storyboard x:Name="myStoryboard">
            <DoubleAnimation From="1" To="6" Duration="00:00:6" 
            Storyboard.TargetName="rectScaleTransform" 
            Storyboard.TargetProperty="ScaleY">
                <DoubleAnimation.EasingFunction>
                    <BounceEase Bounces="2" EasingMode="EaseOut" 
                            Bounciness="2" />
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>
    </StackPanel.Resources>

    <!-- Button that begins animation. -->
    <Button Click="Animation_Begin"
         Margin="2" Content="Begin" />

    <!-- Button that pauses Animation. -->
    <Button Click="Animation_Pause"
         Margin="2" Content="Pause" />

    <!-- Button that resumes Animation. -->
    <Button Click="Animation_Resume"
         Margin="2" Content="Resume" />

    <!-- Button that stops Animation. Stopping the animation 
         returns the ellipse to its original location. -->
    <Button Click="Animation_Stop"
         Margin="2" Content="Stop" />

    <Rectangle Fill="Blue" Width="200" Height="30">
        <Rectangle.RenderTransform>
            <ScaleTransform x:Name="rectScaleTransform" />
        </Rectangle.RenderTransform>
    </Rectangle>

</StackPanel>
private void Animation_Begin(object sender, RoutedEventArgs e)
{
    myStoryboard.Begin();
}
private void Animation_Pause(object sender, RoutedEventArgs e)
{
    myStoryboard.Pause();
}
private void Animation_Resume(object sender, RoutedEventArgs e)
{
    myStoryboard.Resume();
}
private void Animation_Stop(object sender, RoutedEventArgs e)
{
    myStoryboard.Stop();
}

這個範例展示了如何完全用程式碼製作並執行動畫。

<Button Content="Create animation" Click="Button_Click"/>
private void Button_Click(object sender, RoutedEventArgs e)
{
    Create_And_Run_Animation();
}

private void Create_And_Run_Animation()
{
    Storyboard justintimeStoryboard = null;
    // If the storyboard already exists in the resources dictionary,
    // get it and make sure it's stopped.
    if (LayoutRoot.Resources.ContainsKey("justintimeStoryboard"))
    {
        justintimeStoryboard = (Storyboard)LayoutRoot.Resources["justintimeStoryboard"];
        justintimeStoryboard.Stop();
    }
    // Otherwise, create the storyboard and add it to the resources dictionary.
    else
    {
        // Create a red rectangle that will be the target
        // of the animation.
        Rectangle myRectangle = new Rectangle();
        myRectangle.Width = 200;
        myRectangle.Height = 200;
        SolidColorBrush myBrush = new SolidColorBrush(Colors.Red);
        myRectangle.Fill = myBrush;

        // Create the transform.
        TranslateTransform moveTransform = new TranslateTransform();
        moveTransform.X = 0;
        moveTransform.Y = 0;
        myRectangle.RenderTransform = moveTransform;

        // Add the rectangle to the tree.
        LayoutRoot.Children.Add(myRectangle);

        // Create a duration of 2 seconds.
        Duration duration = new Duration(TimeSpan.FromSeconds(2));
        // Create two DoubleAnimations and set their properties.
        DoubleAnimation myDoubleAnimationX = new DoubleAnimation();
        DoubleAnimation myDoubleAnimationY = new DoubleAnimation();
        myDoubleAnimationX.Duration = duration;
        myDoubleAnimationY.Duration = duration;
        justintimeStoryboard = new Storyboard();
        justintimeStoryboard.Duration = duration;
        justintimeStoryboard.Children.Add(myDoubleAnimationX);
        justintimeStoryboard.Children.Add(myDoubleAnimationY);
        Storyboard.SetTarget(myDoubleAnimationX, moveTransform);
        Storyboard.SetTarget(myDoubleAnimationY, moveTransform);

        // Set the X and Y properties of the Transform to be the target properties
        // of the two respective DoubleAnimations.
        Storyboard.SetTargetProperty(myDoubleAnimationX, "X");
        Storyboard.SetTargetProperty(myDoubleAnimationY, "Y");
        myDoubleAnimationX.To = 200;
        myDoubleAnimationY.To = 200;

        // Make the Storyboard a resource.
        LayoutRoot.Resources.Add("justintimeStoryboard", justintimeStoryboard);
    }

    // Begin the animation.
    justintimeStoryboard.Begin();
}

備註

分鏡是 分鏡動畫概念中重要的一門課。 想了解更多概念,請參見 分鏡動畫。

以下作品使用分鏡腳本:

這些屬性並不是唯一定義分鏡的地方。 故事板用於分鏡動畫的典型方式是將故事板定義在 資源 集合中(可能是 Application.Resources 或 FrameworkElement.Resources,或是作為檔案中的資源,例如 Generic.xaml 用於自訂控制項)。 只要它被定義為 XAML 資源,你應該總是為你的故事板指派 x:Name 屬性 值。 之後你可以在程式碼後面以程式變數的方式參考這個名稱。 你需要這個參考,才能實際執行分鏡中包含的動畫,方法是呼叫該分鏡實例的 Begin 方法。 Storyboard 還有其他控制方式,例如 停止 ,可以控制之後的動畫。

Storyboard 繼承了 Timeline 的多個屬性。 這些屬性可以套用到分鏡圖上,或應用在其中的動畫(在 Children 集合中)。 在主故事板上設定 時間軸 屬性,而不是每個動畫,都有優缺點。 如需詳細資訊,請參閱 分鏡腳本動畫。

如果你使用主題動畫,也需要分鏡來控制你加入控制或介面的預設動畫。 主題動畫沒有內建的觸發點,所以你需要把主題動畫放在分鏡中,作為 兒童。 如果分鏡板被用作 VisualState.Storyboard 值,那麼當該視覺狀態載入時,動畫就會執行。 或者,如果是在 VisualTransition.Storyboard 裡,當視覺狀態管理器偵測到該轉換時,動畫就會執行。 這是使用主題動畫最常見的方式,但你也可以在鬆散的分鏡資源中放入,並明確 透過呼叫開始來啟動動畫。

XAML 附加屬性

Storyboard 是多個 XAML 附加屬性的宿主服務類別。 這些功能讓子動畫由分鏡控制,能針對各自的目標元素與屬性,同時仍遵循與父角色相同的控制時間軸與觸發機制。

為了支援 XAML 處理器對附加屬性的存取,並向程式碼暴露等效的 get 與 set 操作,每個 XAML 附加屬性都有一對 Get 與 Set 存取器方法。 另一種在程式碼中取得或設定值的方法是使用相依屬性系統,呼叫 GetValue 或 SetValue ,並將識別欄位傳入相依屬性識別碼。

附屬財產 Description
TargetName 取得或設定物件名稱為動畫。 Storyboard.TargetName 用來引用另一個元素的名稱。 被參考的元素就是動畫應該套用的元素/物件。 此機制是動畫系統基本設計的一部分:它允許動畫資源與 UI 宣告資源分開宣告,並允許將同一動畫定義套用於多種不同的屬性行為。 對於特定動畫的 Storyboard.TargetName 值,你指定目標元素的 Name 或 x:Name 屬性 值,也就是字串。 那個命名元素應該已經存在於 XAML 標記的其他區域。
Name/x:Name 屬性字串的意義由 XAML 命名範圍概念控制。 大多數動畫目標設定場景中,你不必擔心 XAML 名稱範圍的影響,但如果你想鎖定用 XamlReader.Reader.Load 並隨後加入物件樹的模板部分或物件,可能會遇到 XAML 名稱解析問題。 如需詳細資訊,請參閱 XAML 名稱範圍。
目標屬性 取得或設定應該被動畫化的屬性。Storyboard.TargetProperty 針對 Storyboard.TargetName 指定的元素的特定屬性。 你為 Storyboard.TargetProperty 提供的價值包含一個稱為 屬性路徑的概念。 欲了解更多如何指定動畫屬性路徑的資訊,請參閱 Remarks in SetTargetProperty 或 Storyboard.TargetProperty,或 Property-path 語法 主題。

建構函式

名稱 Description
Storyboard()

初始化一個新的 Storyboard 類別實例。

屬性

名稱 Description
AutoReverse

取得或設定一個值,指示時間軸在完成正向迭代後是否會反向播放。

(繼承來源 Timeline)
BeginTime

取得或設定此 時間軸 應開始的時間。

(繼承來源 Timeline)
Children

取得子 時間軸 物件集合。

Dispatcher

它總是在 Windows 應用程式 SDK 應用程式中回傳 null 。 改用 DispatcherQueue 。

(繼承來源 DependencyObject)
DispatcherQueue

得到 DispatcherQueue 這個物件所關聯的那個。 代表 DispatcherQueue 一個功能,即使程式碼是由非 UI 執行緒發起,也能存取 UI DependencyObject 執行緒。

(繼承來源 DependencyObject)
Duration

取得或設定此時間軸播放的時間長度,不計重複次數。

(繼承來源 Timeline)
FillBehavior

取得或設定一個值,指定動畫在達到有效期間結束後的行為。

(繼承來源 Timeline)
RepeatBehavior

他會觸發或設定這個時間線重複的行為。

(繼承來源 Timeline)
SpeedRatio

取得或設定相對於父節點的速率,該 時間軸中時間的進展。

(繼承來源 Timeline)
TargetNameProperty

識別 Storyboard.TargetName XAML 附加屬性。

TargetPropertyProperty

識別 Storyboard.TargetProperty XAML 附加屬性。

附加屬性

名稱 Description
TargetName

取得或設定物件名稱為動畫。

TargetProperty

取得或設定應該被動畫化的屬性。

方法

名稱 Description
Begin()

啟動與分鏡相關的動畫集合。

ClearValue(DependencyProperty)

清除依賴性財產的局部價值。

(繼承來源 DependencyObject)
GetAnimationBaseValue(DependencyProperty)

回傳任何為相依屬性建立的基礎值,適用於動畫未啟用時。

(繼承來源 DependencyObject)
GetCurrentState()

取得 分鏡的時鐘狀態。

GetCurrentTime()

會得到分 鏡目前的動畫時鐘時間。

GetTargetName(Timeline)

從目標元素取得 Storyboard.TargetName XAML attached 屬性的值。

GetTargetProperty(Timeline)

從目標元素取得 Storyboard.TargetProperty XAML attached 屬性的值。

GetValue(DependencyProperty)

回傳 DependencyObject 中相依屬性的當前有效值。

(繼承來源 DependencyObject)
Pause()

暫停與分鏡相關的動畫時鐘。

ReadLocalValue(DependencyProperty)

若設定了本地值,則回傳依賴性質的局部值。

(繼承來源 DependencyObject)
RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback)

註冊一個通知函式,用於監聽此 DependencyObject 實例中特定 DependencyProperty 的變更。

(繼承來源 DependencyObject)
Resume()

恢復與分鏡相關的動畫時鐘或運行時間狀態。

Seek(TimeSpan)

將分鏡移到指定的動畫位置。 當下一次時鐘滴答發生時,故事板會執行所要求的尋址。

SeekAlignedToLastTick(TimeSpan)

立即(同步)將分鏡移至指定動畫位置。

SetTarget(Timeline, DependencyObject)

使指定的 時間軸 鎖定指定的物件。

SetTargetName(Timeline, String)

設定 Storyboard.TargetName XAML attached 屬性的目標元素值。

SetTargetProperty(Timeline, String)

設定目標元素的 Storyboard.TargetProperty XAML 附加屬性的值。

SetValue(DependencyProperty, Object)

將相依屬性的局部值設定在 DependencyObject 上。

(繼承來源 DependencyObject)
SkipToFill()

將分鏡時鐘的當前時間提前到其有效期間結束。

Stop()

會停止分鏡。

UnregisterPropertyChangedCallback(DependencyProperty, Int64)

取消先前透過呼叫 RegisterPropertyChangedCallback 註冊的變更通知。

(繼承來源 DependencyObject)

事件

名稱 Description
Completed

當 分鏡 物件完成播放時會發生。

(繼承來源 Timeline)

適用於

另請參閱