2013-02-16 95 views
0

我想旋轉以及放大並縮小TextBlock。我想使用Stroyboard並在代碼隱藏文件中編寫代碼。故事板不起作用

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1"> 
    <Grid> 
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> 
      <TextBlock Name="rotatingTextBlock" Text="Hello" RenderTransformOrigin="0.5, 0.5"/> 
      <TextBlock Text=" World!"/> 
     </StackPanel> 
    </Grid> 
</Window> 

..

private void Window_Loaded_1(object sender, RoutedEventArgs e) 
{ 
    TransformGroup tc = new TransformGroup(); 
    RotateTransform rotateTransform = new RotateTransform(); 
    tc.Children.Add(rotateTransform); 
    ScaleTransform scaleTransform = new ScaleTransform(); 
    tc.Children.Add(scaleTransform); 

    rotatingTextBlock.RenderTransform = tc; 

    Storyboard storyboard = new Storyboard(); 
    DoubleAnimation rotateAnimation = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(2))); 
    rotateAnimation.AccelerationRatio = 0.15; 
    rotateAnimation.DecelerationRatio = 0.15; 
    Storyboard.SetTarget(rotateAnimation, rotateTransform); 
    Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath(RotateTransform.AngleProperty)); 
    storyboard.Children.Add(rotateAnimation); 

    DoubleAnimation scaleAnimation = new DoubleAnimation(1, 1.2, new Duration(TimeSpan.FromSeconds(1))); 
    scaleAnimation.AccelerationRatio = 0.5; 
    scaleAnimation.DecelerationRatio = 0.5; 
    scaleAnimation.AutoReverse = true; 
    Storyboard.SetTarget(scaleAnimation, scaleTransform); 
    Storyboard.SetTargetProperty(scaleAnimation, new PropertyPath(ScaleTransform.ScaleXProperty)); 
    storyboard.Children.Add(scaleAnimation); 

    storyboard.Begin(); 
} 

我的代碼不能正常工作。我錯過了什麼嗎?

+1

我想你必須將TextBlock設置爲動畫目標:'Storyboard.SetTarget(scaleAnimation,rotatingTextBlock);'。你也可以在xaml中創建你的Storyboard,並用'((Storyboard)FindResource(「SBName」))在Code-Behind中調用它。開始();' – 2013-02-16 13:55:13

+0

嗯,你的見解有幫助。謝謝!但我想知道爲什麼我不能將RotateTransform設置爲目標。 – Gqqnbig 2013-02-16 14:03:48

回答

1

根據Storyboard.SetTarget only works on UIElements, but throws no exception似乎你不能直接定位Transform。如果你用正確的PropertyPath針對TextBlock它的工作原理:

Storyboard storyboard = new Storyboard(); 
DoubleAnimation rotateAnimation = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(2))); 
rotateAnimation.AccelerationRatio = 0.15; 
rotateAnimation.DecelerationRatio = 0.15; 
Storyboard.SetTarget(rotateAnimation, rotatingTextBlock); 
Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath("RenderTransform.Children[0].(RotateTransform.Angle)")); 
storyboard.Children.Add(rotateAnimation); 

DoubleAnimation scaleAnimation = new DoubleAnimation(1, 1.2, new Duration(TimeSpan.FromSeconds(1))); 
scaleAnimation.AccelerationRatio = 0.5; 
scaleAnimation.DecelerationRatio = 0.5; 
scaleAnimation.AutoReverse = true; 
Storyboard.SetTarget(scaleAnimation, rotatingTextBlock); 
Storyboard.SetTargetProperty(scaleAnimation, new PropertyPath("RenderTransform.Children[1].(ScaleTransform.ScaleX)")); 
storyboard.Children.Add(scaleAnimation); 
1

以下工作:

Storyboard storyboard = new Storyboard(); 
DoubleAnimation rotateAnimation = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(2))); 
rotateAnimation.AccelerationRatio = 0.15; 
rotateAnimation.DecelerationRatio = 0.15; 
try { UnregisterName("rotateTransform"); } 
catch { } 
finally { RegisterName("rotateTransform", rotateTransform); } 
Storyboard.SetTargetName(rotateAnimation, "rotateTransform"); 
Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath(RotateTransform.AngleProperty)); 
storyboard.Children.Add(rotateAnimation); 

DoubleAnimation scaleAnimation = new DoubleAnimation(1, 1.2, new Duration(TimeSpan.FromSeconds(1))); 
scaleAnimation.AccelerationRatio = 0.5; 
scaleAnimation.DecelerationRatio = 0.5; 
scaleAnimation.AutoReverse = true; 
try { UnregisterName("scaleTransform"); } 
catch { } 
finally { RegisterName("scaleTransform", scaleTransform); } 
Storyboard.SetTargetName(scaleAnimation, "scaleTransform"); 
Storyboard.SetTargetProperty(scaleAnimation, new PropertyPath(ScaleTransform.ScaleXProperty)); 
storyboard.Children.Add(scaleAnimation); 

storyboard.Begin(this); 

不過,我想LPL的解決辦法,也適用 - 如果LPL的方法將適用於任何「抽象「目標,那麼我認爲我更喜歡它作爲解決方案(以節省註冊名稱等)。 (問題是我不知道如何把這些屬性路徑放在一起!)