2013-10-25 33 views
1

我想在XAML中製作同時生長和旋轉的標籤。 爲什麼我的代碼不工作? (文長,但不轉動)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> 

回答

3

你需要聲明一個RenderTranform(這裏將是一個RotateTransform)爲您LabelStoryboard.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;)

+0

爲什麼TransformGroup?僅僅因爲Blend創造了它?你的例子會更簡單,因此沒有它會更好。 – Clemens

+0

感謝您注意並報告@Clemens。樣品已更新! – AirL