2012-08-09 61 views
2

嗯,我有兩個問題都與動畫有關。動畫無法在Wpf中工作

1)以下代碼不會爲tittle和border設置動畫效果,我會像這樣調用以下代碼.FadeIn(),當然這是UIElement類型的。

public static void FadeIn(this UIElement targetControl) 
    { 
     DoubleAnimation fadeInAnimation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1.5))); 
     Storyboard.SetTarget(fadeInAnimation, targetControl); 
     Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty)); 
     Storyboard sb = new Storyboard(); 
     sb.Children.Add(fadeInAnimation); 
     sb.Begin(); 
    } 

2)這也不起作用,沒有顯示動畫。

public static void SkewAnimation(this UIElement targetControl) 
{ 
    DoubleAnimation skewAnimation = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(3))); 
    Storyboard.SetTarget(skewAnimation, targetControl); 

    Storyboard.SetTargetProperty(skewAnimation, new PropertyPath(SkewTransform.AngleXProperty)); 
    Storyboard sb = new Storyboard(); 
    sb.Children.Add(skewAnimation); 
    sb.Begin(); 
} 
+1

當你說this.FadeIn(),你的意思是你是用它作爲類型的UIElement的擴展方法?那是對的嗎? – Landern 2012-08-09 16:35:37

+1

是的,我使用這個作爲擴展方法 – Haider 2012-08-09 16:40:05

回答

6

爲什麼就不能動畫像這樣:

public static void FadeIn(this UIElement element) 
{ 
    element.BeginAnimation(
     UIElement.OpacityProperty, 
     new DoubleAnimation(0d, 1d, TimeSpan.FromSeconds(1.5))); 
} 

,並提供了元素的RenderTransform屬性設置爲一個SkewTransform:

public static void SkewAnimation(this UIElement element) 
{ 
    ((SkewTransform)element.RenderTransform).BeginAnimation(
     SkewTransform.AngleXProperty, 
     new DoubleAnimation(0d, 360d, TimeSpan.FromSeconds(3d))); 
} 

編輯:這需要像

element.RenderTransform = new SkewTransform(); 

或XAML:

<SomeElement> 
    <SomeElement.RenderTransform> 
     <SkewTransform /> 
    </SomeElement.RenderTransform> 
</SomeElement> 

不知道爲什麼你的淡入將無法正常工作,但你的SkewAnimation不能因屬性路徑工作。沒有爲UIElement定義​​。屬性路徑必須是這樣的(再次提供的RenderTransform設置爲SkewTransform):

new PropertyPath("RenderTransform.(SkewTransform.AngleXProperty)"); 
+1

無法投射'System.Windows.Media.MatrixTransform'類型的對象來鍵入'System.Windows.Media.SkewTransform'。當我打電話給你的歪斜方法和 並在我的歪斜函數時,當我改變你說的下面的異常發生無法解決屬性路徑'RenderTransform。(SkewTransform.AngleXProperty)'中的所有屬性引用。驗證適用的對象是否支持這些屬性。 – Haider 2012-08-09 17:38:03

+1

那麼,你還沒有發佈相關的代碼。我只能猜測你已經放入RenderTransform屬性,或者甚至是RenderTransform。在我的例子中,我假定RenderTransform被設置爲一個SkewTransform實例。這取決於你。 – Clemens 2012-08-09 17:43:56

+1

謝謝,它現在有效,但爲什麼tittle上的動畫不起作用 – Haider 2012-08-09 18:00:03