如何:時鐘的狀態變更時接收通知

時鐘 CurrentStateInvalidated 的事件會在時鐘變成無效時 CurrentState 發生,例如時鐘啟動或停止時。 您可以使用 直接使用 Clock 註冊此事件,也可以使用 註冊 Timeline

在下列範例中,會 Storyboard 使用 和 兩 DoubleAnimation 個 物件,以動畫顯示兩個矩形的寬度。 事件 CurrentStateInvalidated 用來接聽時鐘狀態變更。

範例

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  x:Class="Microsoft.Samples.Animation.TimingBehaviors.StateExample"
  Background="LightGray">
  <StackPanel Margin="20">
  
    <TextBlock 
      Name="ParentTimelineStateTextBlock"></TextBlock>
    <TextBlock 
      Name="Animation1StateTextBlock"></TextBlock>
    <Rectangle 
      Name="Rectangle01"
      Width="100" Height="50" Fill="Orange" />    
    <TextBlock Name="Animation2StateTextBlock"></TextBlock>
    <Rectangle 
      Name="Rectangle02"
      Width="100" Height="50" Fill="Gray" />  
      
    <Button Content="Start Animations" Margin="20">
      <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
          <BeginStoryboard>
            <Storyboard RepeatBehavior="2x" AutoReverse="True"
              CurrentStateInvalidated="parentTimelineStateInvalidated" >
              <DoubleAnimation
                Storyboard.TargetName="Rectangle01"
                Storyboard.TargetProperty="Width"
                From="10" To="200" Duration="0:0:9"
                BeginTime="0:0:1" 
                CurrentStateInvalidated="animation1StateInvalidated"/>
              <DoubleAnimation
                Storyboard.TargetName="Rectangle02"
                Storyboard.TargetProperty="Width"
                From="10" To="200" Duration="0:0:8"
                BeginTime="0:0:1" 
                CurrentStateInvalidated="animation2StateInvalidated" />            
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Button.Triggers>
    </Button>
  
  
  </StackPanel>
</Page>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Microsoft.Samples.Animation.TimingBehaviors
{

    public partial class StateExample : Page
    {

        private void parentTimelineStateInvalidated(object sender, EventArgs args)
        {
            Clock myClock = (Clock)sender;
            ParentTimelineStateTextBlock.Text +=
                myClock.CurrentTime.ToString() + ":"
                + myClock.CurrentState.ToString() + " ";
        }

        private void animation1StateInvalidated(object sender, EventArgs args)
        {

            Clock myClock = (Clock)sender;

            Animation1StateTextBlock.Text +=
                myClock.Parent.CurrentTime.ToString() + ":"
                + myClock.CurrentState.ToString() + " ";
        }

        private void animation2StateInvalidated(object sender, EventArgs args)
        {

            Clock myClock = (Clock)sender;
            Animation2StateTextBlock.Text +=
                myClock.Parent.CurrentTime.ToString() + ":"
                + myClock.CurrentState.ToString() + " ";
        }
    }
}

Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Animation

Namespace Microsoft.Samples.Animation.TimingBehaviors

    Partial Public Class StateExample
        Inherits Page

        Private Sub parentTimelineStateInvalidated(ByVal sender As Object, ByVal args As EventArgs)
            Dim myClock As Clock = CType(sender, Clock)
            ParentTimelineStateTextBlock.Text += myClock.CurrentTime.ToString() & ":" & myClock.CurrentState.ToString() & " "
        End Sub

        Private Sub animation1StateInvalidated(ByVal sender As Object, ByVal args As EventArgs)

            Dim myClock As Clock = CType(sender, Clock)

            Animation1StateTextBlock.Text += myClock.Parent.CurrentTime.ToString() & ":" & myClock.CurrentState.ToString() & " "
        End Sub

        Private Sub animation2StateInvalidated(ByVal sender As Object, ByVal args As EventArgs)

            Dim myClock As Clock = CType(sender, Clock)
            Animation2StateTextBlock.Text += myClock.Parent.CurrentTime.ToString() & ":" & myClock.CurrentState.ToString() & " "
        End Sub
    End Class
End Namespace

下圖顯示動畫在父時間軸( 分鏡腳本 )進行時所輸入的不同狀態。

Clock states for a Storyboard with two animations

下表顯示 Animation1 CurrentStateInvalidated 事件引發的時間

時間(秒) 狀態
1 使用中
10 使用中
19 已停止
21 使用中
30 使用中
39 已停止

下表顯示 Animation2 CurrentStateInvalidated 事件引發的時間

時間(秒) 狀態
1 使用中
9 填充
11 使用中
19 已停止
21 使用中
29 填充
31 使用中
39 已停止

請注意, Animation1 的事件 CurrentStateInvalidated 會在 10 秒時引發,即使其狀態維持不變也一 Active 樣。 這是因為其狀態在 10 秒後變更,但它從 Active 變更為 FillingActive ,然後在相同的刻度中回到 。