我對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