0
我已经在循环中动态创建了一系列图像,以及每个图像的动画和故事板,并且我正在寻找在动画完成后删除图像。图像,动画和故事板存储在数组中以供参考。C#在完成事件中引用动态创建的图像
动画有不同的持续时间,画布上还有其他元素,我不想删除。
这是我必须生成图像,动画和故事代码:
for (int i = 0; i < apples.Length; i++)
{
Image apple = new Image();
apple.Source = new BitmapImage(new Uri(@"pack://application:,,,/fall;component/Resources/apple.png"));
apple.Height = 48;
apple.Width = 48;
int x = r.Next(5, (int)cGame.ActualWidth - 53);
int y = (int)cGame.ActualHeight - 48;
Canvas.SetLeft(apple, x);
Canvas.SetTop(apple, y);
apples[i] = apple;
}
for(int i = 0; i < speeds.Length; i++)
{
DoubleAnimation speed = new DoubleAnimation();
speed.From = 0;
speed.To = cGame.ActualHeight;
speed.Duration = new TimeSpan(0, 0, 0, 0, r.Next(2, 6) * 500);
speeds[i] = speed;
speeds[i].Completed += new EventHandler(aniComplete);
}
for (int i = 0; i < sbs.Length; i++)
{
Storyboard sb = new Storyboard();
sb.Children.Add(speeds[i]);
Storyboard.SetTarget(speeds[i], apples[i]);
Storyboard.SetTargetProperty(speeds[i], new PropertyPath(TopProperty));
sbs[i] = sb;
}
到目前为止,我已经创建了一个完成的事件,并将其连接到每个动画。我使用此删除元素:
canvas.Children.RemoveAt(1);
但它只是按顺序删除元素,跳过我想保留的元素。这些动画随机运行并具有随机的持续时间,因此这些元素很容易就可以按顺序完成其动画。
我将如何引用已完成事件中的特定元素?我尝试引用发件人,但我不知道我应该把它作为(UIElement)似乎没有工作。