1

我想在 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>
4

1 回答 1

3

您需要为您声明一个RenderTranform(这里它将是一个RotateTransformLabel并且您的定义Storyboard.TargetName="myLabel"中缺少。如果您想了解有关 Transforms 的更多信息,您可以在这里DoubleAnimation找到您需要的内容:

变换定义如何将点从一个坐标空间映射或变换到另一个坐标空间。此映射由转换矩阵描述,它是三行和三列 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来创建动画,它真的(真的)简化了这个过程,让你节省了很多时间!这是一个起点;)

于 2013-10-25T16:51:35.817 回答