从代码更新LinearDoubleKeyFrame KeyTime值

时间:2010-08-06 18:21:07

标签: c# wpf

我有一些像这样的xaml:

<UserControl.Resources>
    <Storyboard x:Name="sbLogo" x:Key="onLoadeducLogo" Completed="sbLogo_Completed">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="image">
            <LinearDoubleKeyFrame x:Name="pauseKeyFrame" KeyTime="0:0:2" Value="0"/>
            <LinearDoubleKeyFrame x:Name="fadeInKeyFram" KeyTime="0:0:6" Value="1"/>
            <LinearDoubleKeyFrame x:Name="fadeOutKeyFrame" KeyTime="0:0:12" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>

我想要做的是从C#中的UserControl代码更新KeyTime元素的LinearDoubleKeyFrame值。

我想也许我可以通过x:Name引用这些元素来做到这一点,但我没有取得多大成功。我也想过,也许我可以将值绑定到后面代码中的字段,但也没有成功。

有没有人有任何线索让我朝着正确的方向前进。

由于 菲尔

2 个答案:

答案 0 :(得分:5)

您是如何尝试在代码中引用LinearDoubleKeyFrame个对象的?

我认为你需要做类似的事情:

var storyboard = (Storyboard)FindResource("onLoadeducLogo");
var animation = (DoubleAnimationUsingKeyFrames)storyboard.Children[0];
var keyframe1 = animation.KeyFrames[0];

keyframe1.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0,0,0,1)); // 1 second

答案 1 :(得分:0)

Image creatureImage = new Image();    
Storyboard fadeInFadeOut = new Storyboard();

    DoubleAnimationUsingKeyFrames dbAnimation = new DoubleAnimationUsingKeyFrames();
                dbAnimation.Duration = TimeSpan.FromSeconds(2);
                LinearDoubleKeyFrame lDKF1 = new LinearDoubleKeyFrame();
                lDKF1.Value = 1;
                lDKF1.KeyTime = TimeSpan.FromSeconds(0);
                dbAnimation.KeyFrames.Add(lDKF1);
                //
                LinearDoubleKeyFrame lDKF2 = new LinearDoubleKeyFrame();
                lDKF2.Value = 0.6;
                lDKF2.KeyTime = TimeSpan.FromSeconds(0.5);
                dbAnimation.KeyFrames.Add(lDKF2);
                //
                LinearDoubleKeyFrame lDKF3 = new LinearDoubleKeyFrame();
                lDKF3.Value = 1;
                lDKF3.KeyTime = TimeSpan.FromSeconds(0.5);
                dbAnimation.KeyFrames.Add(lDKF3);
                //
                LinearDoubleKeyFrame lDKF4 = new LinearDoubleKeyFrame();
                lDKF4.Value = 0;
                lDKF4.KeyTime = TimeSpan.FromSeconds(1);
                dbAnimation.KeyFrames.Add(lDKF4);

                Storyboard.SetTarget(dbAnimation, creatureImage);
                Storyboard.SetTargetProperty(dbAnimation, new PropertyPath(Image.OpacityProperty));
                fadeInFadeOut.Children.Add(dbAnimation);