2010-08-14 3 views
2

WP7 앱에 문제가 있습니다. 내가 WP7 애플 리케이션 양식 WPF 샘플 코드를 작성하려고 해요.Windows Mobile 용 Silverlight의 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 클래스없고 스토리 보드는 (SetTarget 방법이 사촌 조금 이상하다) GetTarget 방법이없는 것 같다. 동일한 기능을 사용하기위한 해킹이 있습니까?

+0

그것을 여기

코드입니다 당신이 원하는 기능을 설명한다면 도움이 될 것입니다. – AnthonyWJones

+0

늦게 다시 미안하지만 나는 BSOD를 초 전에 가지고 있었다. http://www.speedyshare.com/files/23809905/BombDropper.7z –

답변

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

많이 있지만 폭탄 completedBomb = (Bomb) StoryboardServices.GetTarget (completedAnimation); 이 null입니다 –

+0

@lukas : a) 설정하기 위해'StoryboardServices.SetTarget'을 사용하지 않았거나 b)'GetTarget'을 호출하고있는 completedAnimation 객체가' SetTarget'을 호출합니다. – AnthonyWJones

+0

아 SetTarget을 하나 설정하는 것을 잊어 버렸습니다. 죄송합니다. 나는이 패턴이 다른 패턴을 사용해야한다고 생각한다. 나는 FPS 형식을 35에서 8로 떨어 뜨린다./ –

관련 문제