2010-09-14 95 views
2

我是WPF的新手。我正在为示例在线测试结果创建一个动画。在哪里我喜欢显示No的参加问题,No的正确答案是动画。我需要在AttendedQuestionEffect()和RightAnswerEffect()之间创建一个小延迟;需要在两个效果之间创建一个延迟Wpf

代码隐藏代码在这里

 public int TotalNoQuestion { get; set; } 
     public int NoOfQuestionAttended { get; set; } 
     public int NoOfRightAnswer { get; set; } 

    public Window1() 
    { 
     InitializeComponent(); 

     TotalNoQuestion = 100; 
     NoOfQuestionAttended = 18; 
     NoOfRightAnswer = 10; 

     stkpnl.Background = CreateLinearGradientBrush(); 

     Storyboard strBrd = new Storyboard(); 
     strBrd.Completed += new EventHandler(strBrd_Completed); 
     DoubleAnimation myDoubleAnimation = new DoubleAnimation(); 
     myDoubleAnimation.From = 10; 
     myDoubleAnimation.To = (TotalNoQuestion *15); 
     myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(2)); 
     Storyboard.SetTargetName(myDoubleAnimation, stkpnl.Name); 
     Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(StackPanel.HeightProperty)); 
     strBrd.Children.Add(myDoubleAnimation); 
     strBrd.Begin(stkpnl); 

    }  

    void strBrd_Completed(object sender, EventArgs e) 
    { 

     for (int i = 1; i < TotalNoQuestion; i++) 
     { 
      Border brd = new Border(); 
      brd.BorderBrush = Brushes.Black; 
      brd.BorderThickness = new Thickness(1.0); 
      TextBlock txt = new TextBlock(); 
      txt.Text = i.ToString(); 
      txt.Height = 15; 
      txt.Width = 200; 
      brd.Child = txt; 
      txt.FontSize = 12; 
      txt.Foreground = Brushes.Black; 
      stkpnl.Children.Add(brd); 
      txt.Background = CreateLinearGradientBrush(); 
      txt.Tag = i.ToString(); 
     } 

     AttendedQuestionEffect(); 
     // Here i need to create delay. 
     RightAnswerEffect(); 
    } 
    void AttendedQuestionEffect() 
    { 
     int index = 1; 
     UIElementCollection ulCollection = stkpnl.Children; 
     foreach (UIElement uiElement in ulCollection) 
     { 
      if (index <= NoOfQuestionAttended) 
      { 
       Border brd = (Border)uiElement; 
       TextBlock txt = (TextBlock)brd.Child; 
       txt.Background = BlinkEffect(Colors.Blue, Colors.SteelBlue, 3000); 
       brd.Child = txt; 
      } 
      index++; 
     } 

    } 

    void RightAnswerEffect() 
    { 
     int index = 1; 
     UIElementCollection ulCollection = stkpnl.Children; 
     foreach (UIElement uiElement in ulCollection) 
     { 
      if (index <= NoOfRightAnswer) 
      { 
       Border brd = (Border)uiElement; 
       TextBlock txt = (TextBlock)brd.Child; 
       txt.Background = BlinkEffect(Colors.Red, Colors.Blue, 1500); 
       brd.Child = txt; 
      } 
      index++; 
     } 

    } 

    private LinearGradientBrush CreateLinearGradientBrush() 
    { 
     LinearGradientBrush brush = new LinearGradientBrush(); 
     brush.StartPoint = new Point(0, 0); 
     brush.EndPoint = new Point(1, 1); 
     brush.GradientStops.Add(new GradientStop(Colors.LightCoral, 0.1)); 
     brush.GradientStops.Add(new GradientStop(Colors.YellowGreen, 0.35)); 
     brush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.86)); 
     return brush; 
    } 

    private SolidColorBrush BlinkEffect(Color startColor, Color endColor,int time) 
    { 
     ColorAnimation myColorAnimation = new ColorAnimation(); 
     myColorAnimation.From = startColor; 
     myColorAnimation.To = endColor; 
     myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(time)); 
     myColorAnimation.AutoReverse = true; 
     myColorAnimation.RepeatBehavior = RepeatBehavior.Forever; 
     SolidColorBrush myBrush = new SolidColorBrush(); 
     myBrush.BeginAnimation(SolidColorBrush.ColorProperty, myColorAnimation); 
     return myBrush; 
    } 







**Xaml code here..** 

<Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="10"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <Border BorderBrush="Black" Grid.Column="1" BorderThickness="1" Width="200" VerticalAlignment="Bottom" HorizontalAlignment="Left"> 
     <StackPanel x:Name="stkpnl" Grid.Column="1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Background="BlueViolet" Width="200" MaxHeight="450" > 
     </StackPanel> 
      </Border> 
    </Grid> 

`

回答

6

你要查找的动画故事板使用关键帧例如here

在XAML这可能看起来像下面......你只需要它翻译成代码,并将其调整到您的needes:

<Storyboard> 
    <DoubleAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="Opacity"> 
     <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/> 
    </DoubleAnimationUsingKeyFrames> 
    <ObjectAnimationUsingKeyFrames BeginTime="0:0:0.5" Storyboard.TargetProperty="Visibility"> 
     <DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Collapsed}"/> 
    </ObjectAnimationUsingKeyFrames> 
</Storyboard> 

该样本T第一淡出的控制,然后隐藏它。您可以通过修改BeginTime属性来增加延迟时间。

相关问题