目标未在启动故事板时设置错误

时间:2013-10-28 11:51:42

标签: c# silverlight xaml windows-phone-8 windows-phone

我正在尝试将基于XAML的故事板转换为背后的代码,以便我可以同时在多个对象上使用故事板的多个实例。我写这个工厂方法为我创建动画,但是当我启动故事板时,我似乎没有设置Target。有人可以帮助我,我也打算教自己一个编写代码隐藏故事板的有效方法。

public static Storyboard CreateSimpleTranslation(double x, double y, TimeSpan timespan, FrameworkElement target)
    {
        // original storyboard
        //<Storyboard x:Name="MoveUp">
        //    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" 
        //        Storyboard.TargetName="itemsControl">
        //        <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
        //        <EasingDoubleKeyFrame KeyTime="0:0:0.02" Value="-122">
        //            <EasingDoubleKeyFrame.EasingFunction>
        //                <CircleEase EasingMode="EaseIn"/>
        //            </EasingDoubleKeyFrame.EasingFunction>
        //        </EasingDoubleKeyFrame>
        //    </DoubleAnimationUsingKeyFrames>
        //</Storyboard> 

        var newSB = new Storyboard();
        var da1 = new DoubleAnimationUsingKeyFrames();
        Storyboard.SetTargetProperty(da1, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateX)"));
        var da2 = new DoubleAnimationUsingKeyFrames();
        Storyboard.SetTargetProperty(da1, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateY)"));

        var ed1 = new EasingDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0)), 
            Value = 0
        };

        var ed2 = new EasingDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(timespan),
            Value = x,
            EasingFunction = new CircleEase() {EasingMode = EasingMode.EaseIn}
        };

        var ed3 = new EasingDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0)), 
            Value = 0
        };

        var ed4 = new EasingDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(timespan),
            Value = y,
            EasingFunction = new CircleEase() {EasingMode = EasingMode.EaseIn}
        };

        da1.KeyFrames.Add(ed1);
        da1.KeyFrames.Add(ed2);
        da2.KeyFrames.Add(ed3);
        da2.KeyFrames.Add(ed4);
        newSB.Children.Add(da1);
        newSB.Children.Add(da2);

        Storyboard.SetTarget(da1, target); 
        Storyboard.SetTarget(da2, target);

        return newSB;
    }

1 个答案:

答案 0 :(得分:1)

尝试按以下格式重新编码,它适合我

public static Storyboard CreateSimpleTranslation(double x, double y, TimeSpan timespan, FrameworkElement target)
        {
                Storyboard storyboard = new Storyboard();

                DoubleAnimationUsingKeyFrames da1 = new DoubleAnimationUsingKeyFrames();
                var ed1 = new EasingDoubleKeyFrame
                {
                    KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0)),
                    Value = 0,
                    EasingFunction = new CircleEase() { EasingMode = EasingMode.EaseIn }
                };
                da1.KeyFrames.Add(ed1);

                Storyboard.SetTarget(da1, target);
                Storyboard.SetTargetProperty(da1, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateX)"));

                storyboard.Children.Add(da1); 
                return storyboard;
       }