void MainWindow_OnFoodAddition(Food source, EventArgs e) { FoodVisual visual = null; Dispatcher.Invoke(new Action(() => visual = new FoodVisual(source, new Ellipse()))); foodVisuals.Add(visual); Dispatcher.Invoke(new Action(() => canvas.Children.Add(visual.getVisual()))); visual.OnSourceEaten += visual_OnSourceEaten; visual.update(); }
void visual_OnSourceEaten(FoodVisual source, EventArgs e) { foodVisuals.Remove(source); Dispatcher.Invoke(new Action(() => canvas.Children.Remove(source.getVisual()))); }