質問

I want to make a simultaneously growing and rotating label in XAML. Why isn't my code working? (text grows, but doesn't rotate)

 <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>
役に立ちましたか?

解決

You need to declare a RenderTranform (here it will be a RotateTransform) for your Label and Storyboard.TargetName="myLabel" are missing into your DoubleAnimation definitions. If you want to know more about Transforms, you will find what you need in here :

A Transform defines how to map, or transform, points from one coordinate space to another coordinate space. This mapping is described by a transformation Matrix, which is a collection of three rows with three columns of Double values.

The following code fixes these issues and should work (tested) :

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

If you can, i would advice you to use Blend to create your animations, it really (really) ease the process and makes you save a lot of time! Here is a starting point ;)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top