适用于 Windows Mobile 的 Silverlight 中的 Storyboard.GetTarget [英] Storyboard.GetTarget in Silverlight for Windows Mobile

查看:12
本文介绍了适用于 Windows Mobile 的 Silverlight 中的 Storyboard.GetTarget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 WP7 应用程序时遇到问题.我正在尝试编写 WP7 应用程序表单 WPF 示例代码.

I have a problem with WP7 app. I'm trying to write WP7 app form WPF sample code.

    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 类,Storyboard 没有 GetTarget 方法(这有点奇怪,因为有 SetTarget 方法).有没有技巧可以让他获得相同的功能?

it seems that there is no ClockGroup class and Storyboard does not have GetTarget method (which is a bit strange cuz there is SetTarget method). Is there a hack to get he same functionality?

推荐答案

我对 WPF 知之甚少,但在 Silverlight 或 WP7 中,Storyboard 的子项属于 TimeLine 类型.此外,StoryBoard 本身会有一个 Completed 事件,您将绑定到该事件.所以至少代码的第一块看起来像:-

I know little of WPF but in Silverlight or WP7 the children of a Storyboard are of type TimeLine. Also a StoryBoard itself would have a Completed event to which you would be binding. So at least the first chunk of the code would look like:-

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.

Its actually quite unusual for Storyboard.SetTarget to be used in Silverlight code. I guess that game code is more likely to generate elements and animations in code and therefore more likely to use SetTarget. If this is what you want to do then you will need to build your own attached property that has both Get and Set, have the changed callback on this property call the 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 代码将变成:-

Now the SetTarget code would become:-

 StoryboardServices.SetTarget(completedAnimation, bomb);

然后您完成的事件可以通过以下方式检索目标:-

Then your completed event can retrieve the target with:-

 Bomb completedBomb = (Bomb)StoryboardServices.GetTarget(completedAnimation);

这篇关于适用于 Windows Mobile 的 Silverlight 中的 Storyboard.GetTarget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆