다음을 통해 공유


방법: 시계의 상태가 변경될 때 알림 받기

업데이트: 2007년 11월

시계가 시작되거나 중지될 때와 같이 시계의 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>
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)가 진행될 때 애니메이션에 적용되는 두 가지 다른 상태를 보여 줍니다.

두 개의 애니메이션이 있는 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로 변경되었기 때문입니다.