Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusable animations

Tags:

c#

wpf

xaml

I am a complete newbie to WPF and XAML.

I have created a simple fade-in and fade-out animation for a textbox:-

<Storyboard x:Key="storyFadeInOutTop" Name="storyFadeInOutTop">
    <DoubleAnimation From="0" To="1" Duration="00:00:01" 
                    Storyboard.TargetName="txtTopCredit" 
                    Storyboard.TargetProperty="Opacity">
    </DoubleAnimation>
    <DoubleAnimation From="10" To="0" Duration="00:00:01" BeginTime="00:00:01"
                    Storyboard.TargetName="blurTop" 
                    Storyboard.TargetProperty="Radius">
    </DoubleAnimation>
    <DoubleAnimation From="0" To="10" Duration="00:00:01" BeginTime="00:00:05"
                    Storyboard.TargetName="blurTop" 
                    Storyboard.TargetProperty="Radius">
    </DoubleAnimation>
    <DoubleAnimation From="1" To="0" Duration="00:00:01" BeginTime="00:00:06"
                    Storyboard.TargetName="txtTopCredit" 
                    Storyboard.TargetProperty="Opacity">
    </DoubleAnimation>
</Storyboard>

What I would like to do is run this storyboard several times within the lifecyle of the animation.

Something like:-

<Storyboard>
   <!-- (Run my fade-in-fade out with BeginTime of 00:00:00) -->
   <StringAnimationUsingKeyFrames Duration="00:00:01" BeginTime="00:00:07"
           Storyboard.TargetName="txtTopCredit" 
           Storyboard.TargetProperty="Text">
          <DiscreteStringKeyFrame Value="Game design and concept by" KeyTime="0:0:1" />
    </StringAnimationUsingKeyFrames>
   <!-- (Run my fade-in-fade out again with BeginTime of 00:00:07) -->
   <StringAnimationUsingKeyFrames Duration="00:00:01" BeginTime="00:00:07"
           Storyboard.TargetName="txtTopCredit" 
           Storyboard.TargetProperty="Text">
          <DiscreteStringKeyFrame Value="Look Ive changed to another credit" KeyTime="0:0:1" />
    </StringAnimationUsingKeyFrames>
    <!-- (etc etc) -->
</Storyboard>

I hope you understand the gist of what I'm trying to do. I know I could just add the code in the storyboard to each section of the above but that would be very tedious. Is there an elegant way to do this?

like image 701
Dark Coder TheDarkInCode Avatar asked Oct 02 '14 11:10

Dark Coder TheDarkInCode


1 Answers

You should be able to set the RepeatBehavior property on the storyboard (inherited from Timeline)

<Storyboard x:Key="storyFadeInOutTop" Name="storyFadeInOutTop" RepeatBehavior="Forever">
    <DoubleAnimation From="0" To="1" Duration="00:00:01" 
                    Storyboard.TargetName="txtTopCredit" 
                    Storyboard.TargetProperty="Opacity">
    </DoubleAnimation>
    <DoubleAnimation From="10" To="0" Duration="00:00:01" BeginTime="00:00:01"
                    Storyboard.TargetName="blurTop" 
                    Storyboard.TargetProperty="Radius">
    </DoubleAnimation>
    <DoubleAnimation From="0" To="10" Duration="00:00:01" BeginTime="00:00:05"
                    Storyboard.TargetName="blurTop" 
                    Storyboard.TargetProperty="Radius">
    </DoubleAnimation>
    <DoubleAnimation From="1" To="0" Duration="00:00:01" BeginTime="00:00:06"
                    Storyboard.TargetName="txtTopCredit" 
                    Storyboard.TargetProperty="Opacity">
    </DoubleAnimation>
</Storyboard>

RepeatBehavior can also be set to a positive integer.

like image 145
Nick Mertin Avatar answered Nov 02 '22 17:11

Nick Mertin