我对WPF知之甚少,但在Silverlight或WP7中,Storyboard
的孩子属于TimeLine
类型。另外一个StoryBoard本身会有一个你将要绑定的Completed
事件。所以至少第一部分的代码看起来像: -
private void storyboard_Completed(object sender, EventArgs e)
{
Storyboard sb = (Storyboard)sender;
DoubleAnimation completedAnimation = (DoubleAnimation)sb.Children[0];
现在的棘手的一点。
其实在Silverlight代码中使用Storyboard.SetTarget
其实很不寻常。我猜游戏代码更有可能在代码中生成元素和动画,因此更有可能使用SetTarget
。如果这是你想要做的,那么你将需要建立你自己的既有Get和Set属性的附属属性,在这个属性上调用回调函数Storyboard.SetTarget
。
下面是代码: -
public static class StoryboardServices
{
public static DependencyObject GetTarget(Timeline timeline)
{
if (timeline == null)
throw new ArgumentNullException("timeline");
return timeline.GetValue(TargetProperty) as DependencyObject;
}
public static void SetTarget(Timeline timeline, DependencyObject value)
{
if (timeline == null)
throw new ArgumentNullException("timeline");
timeline.SetValue(TargetProperty, value);
}
public static readonly DependencyProperty TargetProperty =
DependencyProperty.RegisterAttached(
"Target",
typeof(DependencyObject),
typeof(Timeline),
new PropertyMetadata(null, OnTargetPropertyChanged));
private static void OnTargetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Storyboard.SetTarget(d as Timeline, e.NewValue as DependencyObject);
}
}
现在SetTarget
代码将变为: -
StoryboardServices.SetTarget(completedAnimation, bomb);
然后你完成的事件可以检索的目标: -
Bomb completedBomb = (Bomb)StoryboardServices.GetTarget(completedAnimation);
这可能会帮助你,如果你描述你想达到的功能。 – AnthonyWJones 2010-08-15 12:42:22
对不起,但我在一秒前发生了蓝屏死机。 http://www.speedyshare.com/files/23809905/BombDropper.7z – 2010-08-15 14:06:51