Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF XAML Animation (Newbie)

I am trying to test animation in XAML. My intension was to make a font-size pulse (increase and decrease forever). But when I type in the code below, Visual studio does not recognize the class DoubleAnimation. What am I doing wrong?

<Window x:Class="testingAnimation.MainWindow"
        xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
        Title="MainWindow" Height="350" Width="525">
 <StackPanel>
    <TextBlock Text="HELLO">
       <TextBlock.FontSize>
            <DoubleAnimation />
       </TextBlock.FontSize>
    </TextBlock>
 </StackPanel>
</Window>
like image 437
ragnarius Avatar asked Feb 18 '23 12:02

ragnarius


2 Answers

You need to declare a Storyboard and start it upon load:

 <TextBlock x:Name="Text" Text="Hello!!">
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard Duration="00:00:01" RepeatBehavior="Forever" AutoReverse="True">
                                <DoubleAnimation From="10" To="20" Storyboard.TargetName="Text" Storyboard.TargetProperty="FontSize"/>   
                            </Storyboard>                            
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </TextBlock.Triggers>
    </TextBlock>
like image 156
Federico Berasategui Avatar answered Feb 28 '23 11:02

Federico Berasategui


You need to use Storyboard for running animation -

<TextBlock x:Name="textBlock" Text="HELLO">
     <TextBlock.Triggers>
         <EventTrigger RoutedEvent="FrameworkElement.Loaded">
             <BeginStoryboard>
                 <Storyboard RepeatBehavior="Forever" AutoReverse="True">
                     <DoubleAnimation Storyboard.TargetName="textBlock" 
                               Storyboard.TargetProperty="FontSize"
                               From="10" To="30" 
                               Duration="0:0:1"/>
                  </Storyboard>
             </BeginStoryboard>
          </EventTrigger>
      </TextBlock.Triggers>
</TextBlock>

To learn more about animations follow this link here.

like image 29
Rohit Vats Avatar answered Feb 28 '23 10:02

Rohit Vats