适用于Windows Mobile的Silverlight中的Storyboard.GetTarget

Luk*_*don 2 c# silverlight wpf windows-phone-7

我有WP7应用程序的问题.我正在尝试从WPF示例代码编写WP7应用程序.

    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);
Run Code Online (Sandbox Code Playgroud)

似乎没有ClockGroup类,而Storyboard没有GetTarget方法(这有点奇怪,因为有SetTarget方法).是否有一个hack来获得相同的功能?

Ant*_*nes 7

我对WPF知之甚少,但在Silverlight或WP7中,a的孩子Storyboard都属于类型TimeLine.StoryBoard本身也会有一个Completed你要绑定的事件.所以至少第一块代码看起来像:

private void storyboard_Completed(object sender, EventArgs e)
{
    Storyboard sb = (Storyboard)sender;

    DoubleAnimation completedAnimation = (DoubleAnimation)sb.Children[0];
Run Code Online (Sandbox Code Playgroud)

现在是棘手的一点.

Storyboard.SetTarget在Silverlight代码中使用它实际上很不寻常.我猜游戏代码更有可能在代码中生成元素和动画,因此更有可能使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在SetTarget代码将成为: -

 StoryboardServices.SetTarget(completedAnimation, bomb);
Run Code Online (Sandbox Code Playgroud)

然后,您完成的事件可以检索目标: -

 Bomb completedBomb = (Bomb)StoryboardServices.GetTarget(completedAnimation);
Run Code Online (Sandbox Code Playgroud)