c# - 需要在 Wpf 中的两个效果之间创建延迟

标签 c# wpf

我是 WPF 新手。我正在为示例在线测试结果创建动画。我喜欢以动画形式显示参加问题的数量,正确答案的数量。我需要在 AttendedQuestionEffect() 和 RightAnswerEffect() 之间创建小的延迟;

代码隐藏在这里

        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>

`

最佳答案

您应该使用关键帧查找 Storyboard动画,例如here .

在 Xaml 中,这可能类似于以下内容......您只需将其转换为代码并根据您的需要进行调整:

<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>

此示例首先淡出一个控件,然后将其隐藏。您可以通过修改 BeginTime 属性来添加延迟。

关于c# - 需要在 Wpf 中的两个效果之间创建延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3711577/

相关文章:

c# - 奇怪的 WPF 错误

.net - WPF - TabControl - 防止选择更改

c# - 游戏引擎开发问题

c# - 了解用户是否更改了 DataGrid 中的数据的最佳方法是什么?

c# - 在代码中设置静态资源

c# - 更新对象后刷新 wpf 数据绑定(bind)

wpf - 在 MVVM 中创建 ViewModel 的最佳位置

c# - 升级到 MVC 5 的网站在 ControllerContext 上缺少 DisplayMode 属性

C# 更改列表中的对象

c# - morpho (safran) 生物识别扫描仪设备的 SDK