WPF Animation Pause / Continue

I am experimenting with WPF animation and I am a bit stuck. Here is what I need to do:

MouseOver:

Fade In (Opacity from 0% to 100% in 2 seconds)

MouseOut:

Pause for 2 seconds

Fade Out (Opacity from 100% to 0% in 2 seconds)

I have Fade In and Fade Out effects, but I can’t figure out how to implement Pause, or even if it is possible.

+3
source share
1 answer

Here is some XAML that shows how to do what you need (you can paste all this into Kaxaml to try from:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid Background="Red">  
    <Grid.Triggers>
      <EventTrigger RoutedEvent="Grid.Loaded">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard RepeatBehavior="Forever">
              <DoubleAnimation Storyboard.TargetProperty="Opacity"
                               From="1" To="0" 
                               Duration="0:00:02"
                               BeginTime="0:00:02" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </Grid.Triggers>
  </Grid>
</Page>

The trick is to use BeginTimefor the class DoubleAnimation.

+5
source

Source: https://habr.com/ru/post/1719828/


All Articles