代码之家  ›  专栏  ›  技术社区  ›  philiphobgen

从代码更新LinearDoubleKeyframe keytime值

  •  4
  • philiphobgen  · 技术社区  · 16 年前

    我有一些像这样的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>
    

    我要做的是更新 KeyTime 价值观 LinearDoubleKeyFrame C中后面的用户控件代码中的元素。

    我想也许我可以通过引用这些元素 x:Name 但我没有太大的成功。我还认为也许我可以将这些值绑定到后面代码中的一个字段,但在那里也没有成功。

    有人能给我任何线索把我推向正确的方向吗?

    谢谢 菲尔

    2 回复  |  直到 15 年前
        1
  •  5
  •   Jay    16 年前

    你是如何试图引用 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
    
        2
  •  0
  •   FrenchWolf    15 年前
    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);