Condividi tramite


Procedura: ricevere una notifica quando uno stato dell'orologio viene modificato

L'evento CurrentStateInvalidated di un orologio si verifica quando il relativo oggetto CurrentState diventa non valido, ad esempio quando l'orologio viene avviato o interrotto. È possibile eseguire la registrazione per questo evento utilizzando direttamente un oggetto Clock oppure è possibile eseguire la registrazione tramite un oggetto Timeline.

Nell'esempio seguente, un oggetto Storyboard e due oggetti DoubleAnimation vengono utilizzati per aggiungere un'animazione alla larghezza di due rettangoli. L'evento CurrentStateInvalidated viene utilizzato per essere in ascolto dei cambiamenti di stato dell'orologio.

Esempio

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

Nell'immagine seguente vengono mostrati i diversi stati attraversati dalle animazioni man mano che la sequenza temporale padre (Storyboard) avanza.

Stati di clock per uno storyboard con due animazioni

Nella tabella seguente vengono mostrati i momenti in cui l'evento CurrentStateInvalidated dell'Animazione1 viene generato:

Tempo (secondi)

1

10

19

21

30

39

Stato

Active

Active

Stopped

Active

Active

Stopped

Nella tabella seguente vengono mostrati i momenti in cui l'evento CurrentStateInvalidated dell'Animazione2 viene generato:

Tempo (secondi)

1

9

11

19

21

29

31

39

Stato

Active

Riempimento

Active

Stopped

Active

Riempimento

Active

Stopped

Tenere presente che l'evento CurrentStateInvalidated dell'Animazione1 viene generato a 10 secondi, anche se il relativo stato rimane Active. Questa situazione si verifica perché lo stato è cambiato a 10 secondi, ma è stato modificato da Active a Filling e quindi nuovamente riportato a Active nello stesso ciclo.