2012-04-02 63 views
2

我想獲得這些橢圓增長,但我不知道如何啓動動畫。這是我第一次嘗試WPF動畫,我不太明白它是如何工作的。動畫在C中的對象的高度和寬度#

private void drawEllipseAnimation(double x, double y) 
{ 
    StackPanel myPanel = new StackPanel(); 
    myPanel.Margin = new Thickness(10); 

    Ellipse e = new Ellipse(); 
    e.Fill = Brushes.Yellow; 
    e.Stroke = Brushes.Black; 
    e.Height = 0; 
    e.Width = 0; 
    e.Opacity = .8; 
    canvas2.Children.Add(e); 
    Canvas.SetLeft(e, x); 
    Canvas.SetTop(e, y); 

    DoubleAnimation myDoubleAnimation = new DoubleAnimation(); 
    myDoubleAnimation.From = 0; 
    myDoubleAnimation.To = 10; 
    myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5)); 
    myStoryboard = new Storyboard(); 
    myStoryboard.Children.Add(myDoubleAnimation); 
    Storyboard.SetTargetName(myDoubleAnimation, e.Name); 
    Storyboard.SetTargetProperty(myDoubleAnimation, new  PropertyPath(Ellipse.HeightProperty)); 
    Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Ellipse.WidthProperty)); 
} 
+0

僅供參考,這不是「C#動畫」,它是「WPF動畫」。 – 2012-04-02 21:15:09

回答

8

這裏不需要Storyboard。只要做到

e.BeginAnimation(Ellipse.WidthProperty, myDoubleAnimation); 
e.BeginAnimation(Ellipse.HeightProperty, myDoubleAnimation); 

如果你真的需要一個Storyboard做到這一點,你必須增加單獨的動畫,每動畫屬性之一,故事板。當您不使用名稱時,您必須撥打SetTarget而不是SetTargetName。最後,你需要通過調用Begin來啓動故事板:

DoubleAnimation widthAnimation = new DoubleAnimation 
{ 
    From = 0, 
    To = 10, 
    Duration = TimeSpan.FromSeconds(5) 
}; 

DoubleAnimation heightAnimation = new DoubleAnimation 
{ 
    From = 0, 
    To = 10, 
    Duration = TimeSpan.FromSeconds(5) 
}; 

Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Ellipse.WidthProperty)); 
Storyboard.SetTarget(widthAnimation, e); 

Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Ellipse.HeightProperty)); 
Storyboard.SetTarget(heightAnimation, e); 

Storyboard s = new Storyboard(); 
s.Children.Add(widthAnimation); 
s.Children.Add(heightAnimation); 
s.Begin(); 
+0

GENIUS!謝謝你,我討厭成爲這樣的小菜鳥。我知道有一個簡單的答案。這很好。 – miltonjbradley 2012-04-02 21:41:33