■ Storyboard 클래스를 사용해 코드로 구현하는 방법을 보여준다.
▶ 예제 코드 (XAML)
1 2 3 |
<Canvas x:Name="canvas" /> |
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; using System.Windows; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; Rectangle rectangle = new Rectangle(); rectangle.Width = 200d; rectangle.Height = 200d; Color color = Color.FromArgb(255, 255, 0, 0); SolidColorBrush solidColorBrush = new SolidColorBrush(); solidColorBrush.Color = color; rectangle.Fill = solidColorBrush; this.canvas.Children.Add(rectangle); Duration duration = new Duration(TimeSpan.FromSeconds(2)); DoubleAnimation leftDoubleAnimation = new DoubleAnimation(); DoubleAnimation topDoubleAnimation = new DoubleAnimation(); leftDoubleAnimation.Duration = duration; topDoubleAnimation.Duration = duration; Storyboard storyboard = new Storyboard(); storyboard.Children.Add(leftDoubleAnimation); storyboard.Children.Add(topDoubleAnimation ); Storyboard.SetTarget(leftDoubleAnimation, rectangle); Storyboard.SetTarget(topDoubleAnimation , rectangle); Storyboard.SetTargetProperty(leftDoubleAnimation, new PropertyPath("(Canvas.Left)")); Storyboard.SetTargetProperty(topDoubleAnimation , new PropertyPath("(Canvas.Top)" )); leftDoubleAnimation.To = 200d; topDoubleAnimation.To = 200d; this.canvas.Resources.Add("StoryboardKey", storyboard); storyboard.Begin(); |
※ WPF에서는 Storyboard.SetTarget 메소드가 없기 때문에 Container 객체(예 : Page)에 대해 NameScope 객체를 설정하고 애니메이션 대상 객체명을 등록한 후 Storyboard.SetTargetName 메소드를 사용해 대상 객체명을 지정한다.