共用方式為


HOW TO:時鐘的狀態變更時接收通知

當時鐘的 CurrentState 無效時,例如時鐘啟動或停止時,就會發生時鐘的 CurrentStateInvalidated 事件。 您可以直接使用 Clock 註冊這個事件,或是使用 Timeline 進行註冊。

下列範例會使用一個 Storyboard 和兩個 DoubleAnimation 物件來建立兩個矩形寬度的動畫。 CurrentStateInvalidated 事件用於接聽時鐘狀態的變更。

範例

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="https://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>

Imports System
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
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() + " ";                 
        }
    }
}

下圖顯示隨著父項目時間表 (Storyboard) 進行時動畫進入的不同狀態。

具有兩個動畫之腳本的時鐘狀態

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

時間 (秒)

1

10

19

21

30

39

狀態

作用中

作用中

已停止

作用中

作用中

已停止

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

時間 (秒)

1

9

11

19

21

29

31

39

狀態

作用中

填入中

作用中

已停止

作用中

填入中

作用中

已停止

請注意,Animation1 的 CurrentStateInvalidated 事件在第 10 秒引發,即使狀態維持在 Active。 這是因為它的狀態在第 10 秒變更,但是在同一秒間從 Active 變更為 Filling,並變更回 Active