方法: クロックの状態が変化したときに通知を受け取る

クロックの CurrentStateInvalidated イベントは、クロックが開始または停止されるなど、その CurrentState が無効になったときに発生します。 このイベントには、Clock を使用して直接登録することも、Timeline を使用して登録することもできます。

次の例では、2 つの四角形の幅をアニメーション化するために、Storyboard を 1 つと DoubleAnimation オブジェクトを 2 つ使用しています。 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

次の表は、Animation1CurrentStateInvalidated イベントが発生する時刻を示しています。

時間 (秒) State
1 アクティブ
10 アクティブ
19 停止済み
21 アクティブ
30 アクティブ
39 Stopped

次の表は、Animation2CurrentStateInvalidated イベントが発生する時刻を示しています。

時間 (秒) State
1 アクティブ
9 フィル
11 アクティブ
19 停止済み
21 アクティブ
29 フィル
31 アクティブ
39 停止済み

Animation1 の状態が Active のままであっても、その CurrentStateInvalidated イベントは 10 秒のときに発生することに着目してください。 これは、それの状態が 10 秒のときに変更されたけれども、同じティック内で Active から Filling に変更され、その後 Active に戻っているためです。