WPF XAML动画(新手)

时间:2012-11-12 19:50:22

标签: wpf xaml animation

我正在尝试在XAML中测试动画。我的意图是制作一个字体大小的脉冲(永远增加和减少)。但是当我输入下面的代码时,Visual Studio无法识别类DoubleAnimation。我做错了什么?

<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>

2 个答案:

答案 0 :(得分:6)

您需要声明Storyboard并在加载时启动它:

 <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>

答案 1 :(得分:2)

您需要使用Storyboard来运行动画 -

<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>

要了解有关动画的详情,请点击此链接here