2010-08-14 58 views
2

我有一个WP7应用程序的问题。我正在尝试编写WPF应用程序窗体WPF示例代码。Silverlight for Windows Mobile中的Storyboard.GetTarget

private void storyboard_Completed(object sender, EventArgs e) 
    { 
     ClockGroup clockGroup = (ClockGroup)sender; 

     // Get the first animation in the storyboard, and use it to find the 
     // bomb that's being animated. 
     DoubleAnimation completedAnimation = (DoubleAnimation)clockGroup.Children[0].Timeline; 
     Bomb completedBomb = (Bomb)Storyboard.GetTarget(completedAnimation); 

似乎没有ClockGroup类和故事板没有了getTarget方法(这是一个有点奇怪的Cuz有SetTarget法)。是否有黑客获得他相同的功能?

+0

这可能会帮助你,如果你描述你想达到的功能。 – AnthonyWJones 2010-08-15 12:42:22

+0

对不起,但我在一秒前发生了蓝屏死机。 http://www.speedyshare.com/files/23809905/BombDropper.7z – 2010-08-15 14:06:51

回答

7

我对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); 
+0

thx很多,但炸弹completedBomb =(炸弹)StoryboardServices.GetTarget(completedAnimation); 是空的 – 2010-08-15 14:28:45

+0

@lukas:然后或者a)您没有使用'StoryboardServices.SetTarget'来设置它或b)您调用'GetTarget'的completedAnimation对象与传递给''' SetTarget'。 – AnthonyWJones 2010-08-15 15:50:18

+0

哦,我忘了设置一个SetTarget。抱歉。 我想我需要使用不同的图案因为这是一个严肃的缓慢。我得到FPS从35降到8:/ – 2010-08-15 16:31:47