2010-08-06 166 views
3

一个LinearDoubleKeyFrame KeyTime值我有一些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> 

我想要做的就是更新从后面的用户控件代码LinearDoubleKeyFrame元素在C#中的KeyTime值。

我想也许我可以通过参考这些元素x:Name来做到这一点,但我没有太大的成功。我也想过,也许我可以将值绑定到代码背后的一个字段,但在那里也没有成功。

有没有人有任何线索推动我走向正确的方向。

感谢 菲尔

回答

4

你如何试图引用代码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 
+0

辉煌,当场。我的错误是尝试FindResource的“pauseKeyFrame” - 当然现在我看到你的答案,我意识到故事板是资源。谢谢。 – philhobgen 2010-08-06 21:11:26

-1
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); 
+1

欢迎来到StackOverflow!也许有些解释会为你的文章添加上下文。感谢您花时间回答! – Miquel 2012-11-28 20:15:54