我必須實現一個道路交匯處簡單程序。路口的圖像被設置爲WPF網格的背景屬性,並且我在隊列中有ArrayLists來表示每輛車,起源街道和目的地街道的顏色。
現在,我需要將汽車製成動畫橢圓形,並且我需要每輛汽車在私密車離開屏幕後開始動作。
我正在使用下面的代碼,但它只動畫第一輛車。
解決方案是什麼?因此使用wpf動畫橢圓
private void button1_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < queue.Count; i++)
{
ArrayList car = (ArrayList)queue[i];
int id = Convert.ToInt32(car[0]);
int color = Convert.ToInt32(car[1]);
int from= Convert.ToInt32(car[2]);
int to = Convert.ToInt32(car[3]);
Ellipse myEllipse = new Ellipse();
if (color == 0)
{
myEllipse.Stroke = System.Windows.Media.Brushes.Green;
myEllipse.Fill = System.Windows.Media.Brushes.Green;
}
else {
myEllipse.Stroke = System.Windows.Media.Brushes.Blue;
myEllipse.Fill = System.Windows.Media.Brushes.Blue;
}
myEllipse.HorizontalAlignment = HorizontalAlignment.Left;
myEllipse.VerticalAlignment = VerticalAlignment.Center;
myEllipse.Width = 45;
myEllipse.Height = 45;
myGrid.Children.Add(myEllipse);
DoubleAnimation da = new DoubleAnimation();
da.From = from;
da.To = to;
da.Duration = new Duration(TimeSpan.FromSeconds(1));
TranslateTransform tt = new TranslateTransform();
myEllipse.RenderTransform = tt;
tt.BeginAnimation(TranslateTransform.XProperty, da);
}
}
您使用的是.NET 3.0中引入的WPF,以及ArrayLists,它們自.NET 2.0推出以來已經過時了嗎? –