以编程方式完全创建故事板C#

时间:2016-06-08 14:13:17

标签: c# animation storyboard blend

我试图用C#完全创建一个故事板,根本没有XAML,但是我遇到了麻烦(SetTarget和SetTargetProperty)..这里是我的代码

我只是在用户导航到我的UserControl时动画, 它从0不透明度变为100,从900 TransformX变为0,在0.5秒内。

我真的很感激设置这两个参数的任何帮助,一整天都没有运气!

    public void Designer()
    {
        Control_ = new UserControl();

        Control_.HorizontalAlignment = HorizontalAlignment.Stretch;
        Control_.VerticalAlignment = VerticalAlignment.Stretch;

        Control_.Name = "Control_";
        this.AddChild(Control_);

        CreateStoryboard();
    }

    public void CreateStoryboard()
    {
        fadeinBoard = new Storyboard();
        Duration duration = new Duration(TimeSpan.FromMilliseconds(5));
        fadeinBoard.Duration = duration;

        DoubleAnimationUsingKeyFrames animOpacity = new DoubleAnimationUsingKeyFrames();
        DoubleAnimationUsingKeyFrames animTransform = new DoubleAnimationUsingKeyFrames();

        animOpacity.Duration = duration;
        animTransform.Duration = duration;

        //Transform Function
        KeyTime ktime1 = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.5));
        PowerEase pow = new PowerEase();
        pow.Power = 5;
        pow.EasingMode = EasingMode.EaseOut;
        EasingDoubleKeyFrame keyFrame1 = new EasingDoubleKeyFrame(0, ktime1, pow);

        //Opacity Function
        KeyTime ktime2 = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.5));
        ExponentialEase expo = new ExponentialEase();
        expo.Exponent = 3;
        expo.EasingMode = EasingMode.EaseOut;
        EasingDoubleKeyFrame keyFrame2 = new EasingDoubleKeyFrame(1, ktime2, expo);

        animOpacity.KeyFrames.Add(keyFrame1);
        animTransform.KeyFrames.Add(keyFrame2);

        // MY PROBLEM IS HERE
        Storyboard.SetTarget(???, ???);
        Storyboard.SetTarget(???, ???);
        Storyboard.SetTargetProperty(???, ???);
        Storyboard.SetTargetProperty(???, ???)));

        fadeinBoard.Children.Add(animOpacity);
        fadeinBoard.Children.Add(animTransform);

        Control_.Resources.Add("fader", fadeinBoard);
    }

1 个答案:

答案 0 :(得分:2)

这对我有用,具体如下:

    Storyboard.SetTarget(animOpacity, Control_);
    Storyboard.SetTarget(animTransform, Control_);
    Storyboard.SetTargetProperty(animOpacity, new PropertyPath("Opacity"));
    Storyboard.SetTargetProperty(animTransform, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

请注意从OpacityProperty到Opacity的修正,以及完全限定的属性语法 - 我暂时将其配置为在ScaleY上工作。

只有在您可以使用提供的路径访问的控件中添加了RenderTransform时,这才有效,例如:如下:

    ScaleTransform myScaleTransform = new ScaleTransform();
    myScaleTransform.ScaleY = 1;
    myScaleTransform.ScaleX = 1;
    TransformGroup myTransformGroup = new TransformGroup();
    myTransformGroup.Children.Add(myScaleTransform);
    Control_.RenderTransform = myTransformGroup;