WPF实现-倒计时关键帧模拟消息发送

一、实现 

 使用StringAnimationUsingKeyFrames字符串动画关键帧来实现

示例代码

        private StringAnimationUsingKeyFrames myAnimation;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ViewModels.SendViewModel();
            myAnimation = new StringAnimationUsingKeyFrames();
            myAnimation.FillBehavior = FillBehavior.Stop;
            for (int i = 6; i >= 0; i--)
            {
                myAnimation.KeyFrames.Add(
                    new DiscreteStringKeyFrame()
                    {
                        Value = $"{i}s后可再次发送",
                        KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(6 - i))
                    });
            }
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            probar.Value = 0;//同时模拟进度条
            btn.IsEnabled = false;
            btn.Foreground = Brushes.Black;
            myAnimation.Completed += (s, arg) =>
            {
                btn.IsEnabled = true;
            };
            btn.BeginAnimation(Button.ContentProperty, myAnimation);
        }

效果图片:

环形进度条制作点击WPF动画制作_扩展_进度条制作

二、添加加载进度条

示例代码:

GeometryCombineMode="Intersect":取交集

            DoubleAnimation mydbAnimation = new DoubleAnimation();
            mydbAnimation.From = -50;
            mydbAnimation.To = 400;
            mydbAnimation.Duration = new Duration(TimeSpan.FromSeconds(6));
            tranForm.BeginAnimation(TranslateTransform.XProperty, mydbAnimation);
        <Rectangle Width="400"
                   Height="20"
                   Margin="190,349,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"
                   Fill="LightGray" />
        <Path Name="path"
              Width="400"
              Height="20" VerticalAlignment="Center" HorizontalAlignment="Center"
              Margin="190,349,0,0"
              Fill="DeepPink">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Intersect">
                    <CombinedGeometry.Geometry1>
                        <RectangleGeometry Rect="0,0,400,20" />
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry Rect="0,0,50,20">
                            <RectangleGeometry.Transform>
                                <TranslateTransform x:Name="tranForm" X="-6" />
                            </RectangleGeometry.Transform>
                        </RectangleGeometry>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>

效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF中的DispatcherTimer是一种用于定时器任务的计时器类,它允许我们在UI线程上执行指定的操作。当使用DispatcherTimer进行计时时,如果定时器的Interval属性设置得太小,计时会变得非常快。 造成DispatcherTimer计时跳的很快的原因是定时器的Interval属性设置过小。Interval属性表示定时器触发Tick事件的时间间隔。如果Interval设置得非常小,比如设置为1毫秒,那么每经过1毫秒,Tick事件就会被触发一次。在较短的时间间隔内,大量的Tick事件会被触发,导致计时看起来非常快速。 解决这个问题的方法是适当地调整DispatcherTimer的Interval属性,将其设置为合理的数值。可以根据需要的计时速度和精度来设定Interval的值。如果希望计时更加平滑,可以将Interval设置为较大的值,比如100毫秒,这样每次Tick事件的触发间隔就较长。 另外一个可能的原因是在Tick事件中执行的操作较为耗时,导致计时看起来跳的很快。可以通过优化代码或将耗时较长的操作放到后台线程中执行来解决这个问题。使用后台线程可以避免在UI线程上执行耗时操作,从而提高计时的稳定性和准确性。 总之,要解决DispatcherTimer计时跳的很快的问题,需要注意Interval属性的设置和Tick事件中的操作耗时,以确保定时器的触发间隔和操作的执行速度能够达到预期的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值