2010-08-14 54 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

相關問題