WPF storyboard event does not fire

I have a simple storyboard that repeats and automatically reverses. When it reaches the end and automatically changes, I would like to fire the event in code. The same thing when it repeats. How can i do this? Ultimately, I play a wav file during these two events. Thank.

+3
source share
1 answer

Animated WPF animations are controlled by AnimationClock (sort of like a fancy timer). AnimationClock has a CurrentProgress property, which ranges from 0 to 1; where 0 is the starting point and 1 is the ending point. A repeating storyboard gradually changes CurrentProgress from 0 to 1 to 0 ... 1 /

AnimationClock , CurrentTimeInvalidated. AnimationClock. CurrentProgress. , , CurrentProgress, , 0 1. . , , .

xaml:

<Grid x:Name="uxGrid" Background="White">
    <Grid.Triggers>
        <EventTrigger RoutedEvent="Grid.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Storyboard.TargetName="uxGrid" Changed="ColorAnimation_Changed" CurrentTimeInvalidated="ColorAnimation_CurrentTimeInvalidated"  Storyboard.TargetProperty="Background.Color" From="Blue" To="Green" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>
</Grid>

:

private double? _clockLastProgress;  // Tracks Trend
private bool    _clockLastDecreased; // Tracks Trend Direction

private void ColorAnimation_CurrentTimeInvalidated(object sender, EventArgs e)
{
    AnimationClock clock = sender as AnimationClock;

    if (clock != null && clock.CurrentProgress.HasValue)
    {
        if (!_clockLastProgress.HasValue)
        {
            // Your Code Here
        }
        else
        {
            if (_clockLastDecreased)
            {
                if (clock.CurrentProgress > _clockLastProgress)
                {
                    // Your Code Here
                    _clockLastDecreased = false;
                }
            }
            else
            {
                if (clock.CurrentProgress < _clockLastProgress)
                {
                    // Your Code Here
                    _clockLastDecreased = true;
                }
            }
        }

        _clockLastProgress = clock.CurrentProgress.Value;
    }
}
+3

All Articles