我正在 WPF 上制作游戏SizzlingHot。我有一个画布,可以在其中加载水果掉落的图像。当我单击 startButton 时,它应该为落下的水果创建一个带有 doubleAnimation 的故事板。我在方法中有以下代码:
NameScope.SetNameScope(this, new NameScope());
Cherry cherry = new Cherry(); // I get the image from this class
// GameCanvas.Children.Add(cherry.FruitImage); // idk if this should be here and its invalid because the parameter in the parantheses shoud be UI element it does not allow BitmapImage
DoubleAnimation myDoubleAnimation = new DoubleAnimation(100, 500, new Duration(TimeSpan.FromSeconds(5)));
myDoubleAnimation.From = -150;
myDoubleAnimation.To = 500;
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(Canvas.Top)"));
Storyboard.SetTarget(myDoubleAnimation, cherry.FruitImage);
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);
GameCanvas.Resources.Add(myStoryboard,cherry.FruitImage);
myStoryboard.Begin(this, true);
当我运行它时,在最后一行,它给了我一个
InvalidOperationException - 由于对象的当前阶段,操作无效。
当我调试它时,我不明白这里似乎是什么问题我可以看到找到了图像。