xaml旋转并增长标签动画

时间:2013-10-25 16:28:21

标签: c# wpf xaml rotation rotatetransform

我想在XAML中制作一个同时增长和旋转的标签。 为什么我的代码不起作用? (文字增长,但不会旋转)

 <Window 
  xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
  Title = "Animation" 
  Height = "600" Width = "1000" WindowStartupLocation="CenterScreen">

  <Window.Resources>
    <Storyboard x:Key="Storyboard">
            <DoubleAnimation 
            Storyboard.TargetProperty="FontSize"
            From = "12" To = "200" Duration = "0:0:4"
            RepeatBehavior = "Forever" AutoReverse="True"/>
            <DoubleAnimation
            Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
            From="0" To="360" Duration="0:0:4"
            RepeatBehavior="Forever"/>
        </Storyboard>
  </Window.Resources>

    <Label x:Name="myLabel" Content = "Some text">
      <Label.Triggers>
        <EventTrigger RoutedEvent="Label.Loaded">
          <EventTrigger.Actions>
            <BeginStoryboard Storyboard="{StaticResource Storyboard}"/>
          </EventTrigger.Actions>
        </EventTrigger>
      </Label.Triggers>
    </Label>

</Window>

1 个答案:

答案 0 :(得分:3)

您需要为Label声明RenderTranform(此处为RotateTransform},Storyboard.TargetName="myLabel"定义中缺少DoubleAnimation。 如果您想了解有关变换的更多信息,可以在here中找到所需内容:

  

变换定义了如何从一个点映射或变换点   将空间坐标到另一个坐标空间。这种映射是   由转换矩阵描述,它是三个的集合   具有三列Double值的行。

以下代码修复了这些问题,应该可以运行(测试):

 <Window 
  xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
  Title = "Animation" 
  Height = "600" Width = "1000" WindowStartupLocation="CenterScreen">

  <Window.Resources>
    <Storyboard x:Key="Storyboard">
            <DoubleAnimation 
            Storyboard.TargetProperty="FontSize" Storyboard.TargetName="myLabel"
            From = "12" To = "200" Duration = "0:0:4"
            RepeatBehavior = "Forever" AutoReverse="True"/>
                     <DoubleAnimation
            Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" Storyboard.TargetName="myLabel"
            From="0" To="360" Duration="0:0:4"
            RepeatBehavior="Forever"/>
        </Storyboard>
  </Window.Resources>

  <StackPanel>
    <Label x:Name="myLabel" Content = "Some text" RenderTransformOrigin="0.5,0.5">
        <Label.RenderTransform>
             <RotateTransform/>
        </Label.RenderTransform>
      <Label.Triggers>
        <EventTrigger RoutedEvent="Label.Loaded">
          <EventTrigger.Actions>
            <BeginStoryboard Storyboard="{StaticResource Storyboard}"/>
          </EventTrigger.Actions>
        </EventTrigger>
      </Label.Triggers>
    </Label>
    </StackPanel>
</Window>

如果可以的话,我会建议您使用Blend创建动画,这真的(真的)简化了过程,让您节省了大量时间! Here is a starting point;)