MainWindow.xaml.csの全コード
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace WpfAnimation2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
Ellipse myEllipse = new Ellipse();
myEllipse.HorizontalAlignment = HorizontalAlignment.Left;
myEllipse.VerticalAlignment = VerticalAlignment.Top;
myEllipse.Stroke = Brushes.Aquamarine;
myEllipse.Width = 20;
myEllipse.Height = 20;
myEllipse.Fill = Brushes.Coral;
Canvas1.Children.Add(myEllipse);
DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
myDoubleAnimation1.From = -10;
myDoubleAnimation1.To = 390;
Duration duration1 = new Duration(TimeSpan.FromSeconds(0.3));
myDoubleAnimation1.Duration = duration1;
myDoubleAnimation1.AutoReverse = true;
myDoubleAnimation1.RepeatBehavior = RepeatBehavior.Forever;
Storyboard1.Children.Add(myDoubleAnimation1);
Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(Canvas.Left)"));
Storyboard.SetTarget(myDoubleAnimation1, myEllipse);
DoubleAnimation myDoubleAnimation2 = new DoubleAnimation();
myDoubleAnimation2.From = -10;
myDoubleAnimation2.To = 290;
Duration duration2 = new Duration(TimeSpan.FromSeconds(0.5));
myDoubleAnimation2.Duration = duration2;
myDoubleAnimation2.AutoReverse = true;
myDoubleAnimation2.RepeatBehavior = RepeatBehavior.Forever;
Storyboard1.Children.Add(myDoubleAnimation2);
Storyboard.SetTargetProperty(myDoubleAnimation2, new PropertyPath("(Canvas.Top)"));
Storyboard.SetTarget(myDoubleAnimation2, myEllipse);
Storyboard1.Begin();
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
Storyboard1.Stop();
}
}
}