Storyboard.GetTarget in Silverlight for Windows Mobile

I have a problem with a WP7 application. I am trying to write a WPF sample for a WPF WPF application.

    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 being animated.
        DoubleAnimation completedAnimation = (DoubleAnimation)clockGroup.Children[0].Timeline;
        Bomb completedBomb = (Bomb)Storyboard.GetTarget(completedAnimation);

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

+2
source share
1 answer

I don't know much about WPF, but in Silverlight or WP7, the children of Storyboardare of type TimeLine. Also, the StoryBoard itself will have an event Completedthat you would attach to. Thus, at least the first code fragment will look like this: -

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

    DoubleAnimation completedAnimation = (DoubleAnimation)sb.Children[0];

Now for the difficult bit.

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);
    }
}

SetTarget : -

 StoryboardServices.SetTarget(completedAnimation, bomb);

: -

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

Source: https://habr.com/ru/post/1789765/


All Articles