倒计时控件

最近做一个WPF小项目需要使用到计时器,因此写了一个计时控件,记录下来,以便下次使用。

前台的XAML:

<UserControl x:Class="Test.CountDown"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="110" d:DesignWidth="150">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="43*" />
            <ColumnDefinition Width="13"/>
            <ColumnDefinition Width="43*" />
            <ColumnDefinition Width="13"/>
            <ColumnDefinition Width="43*" />
        </Grid.ColumnDefinitions>
        <TextBlock Text="00" Name="HourArea"  VerticalAlignment="Center" FontSize="30" Background="Transparent" Grid.Column="0" Foreground="DarkOrange" />
        <TextBlock Text=":" Name="HourSplitMinute"  VerticalAlignment="Center" FontSize="30" Background="Transparent" Grid.Column="1" Foreground="DarkOrange" />
        <TextBlock Text="00" Name="MinuteArea" VerticalAlignment="Center" FontSize="30" Background="Transparent" Grid.Column="2" Foreground="DarkOrange" />
        <TextBlock Text=":" Name="MinuteSplitSecond"  VerticalAlignment="Center" FontSize="30" Background="Transparent" Grid.Column="3" Foreground="DarkOrange" />
        <TextBlock Text="00"  Name="SecondArea" VerticalAlignment="Center" FontSize="30" Background="Transparent" Grid.Column="4" Foreground="DarkOrange" />
    </Grid>
</UserControl>

后台的逻辑:

public partial class CountDown : UserControl
    {
        public DispatcherTimer timer;
        public Process pro;
        public Stopwatch sw = new Stopwatch();
        public int seconds;

        public CountDown()
        {
            InitializeComponent();
            pro = new Process();

            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += new EventHandler(timer_Tick);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = new TimeSpan (0,0,seconds);
            pro.totalSecond = (int)(ts - sw.Elapsed).TotalSeconds;
            if (pro .totalSecond > 0)
            {
                HourArea.Text = pro.GetHour();
                MinuteArea.Text = pro.GetMinute();
                SecondArea.Text = pro.GetSecond();
            }
            else
            {
                timer.Stop();
                sw.Stop ();
                sw.Reset();
                SecondArea.Text = string.Format("{0:D2}", 0);
            }
        }

    }

    public class Process
    {
        public int totalSecond;

        //获取小时字符串
        public string GetHour()
        {
            return string.Format("{0:D2}", totalSecond / 3600);
        }

        //获取分钟字符串
        public string GetMinute()
        {
            return string.Format("{0:D2}", (totalSecond / 60 - ((int)(totalSecond / 3600) * 60)));
        }

        //获取秒字符串
        public string GetSecond()
        {
            return string.Format("{0:D2}", totalSecond % 60);
        }
    }

调用:

this.countDown1.seconds = 300;//传入倒计时总时间(秒)
            this.countDown1.timer.Start();
            this.countDown1.sw.Start();
时间: 2024-08-05 20:08:11

倒计时控件的相关文章

android自定义倒计时控件示例

这篇文章主要介绍了Android秒杀倒计时自定义TextView示例,大家参考使用吧 自定义TextView控件TimeTextView代码: 复制代码 代码如下: import android.content.Context;import android.content.res.TypedArray;import android.graphics.Paint;import android.text.Html;import android.util.AttributeSet;import and

Android自定义倒计时控件

序: 最近越来越多的APP都是用手机号注册,一是为了方便用户记忆,二是为了增加用户账户的安全性.在我们进行交易操作或者修改密码等操作的时候,这时候需要输入短信验证码.这个控件会需要有倒计时的功能,这里主要总结常见的几种实现方式. 1.Android中实现倒计时的方法 第一种:直接用Handler的消息机制来实现 这种方式感觉是最原始的,这里不多说. 第二种:Timer和TimerTask 基本使用:获得Timer和TimerTask对象,然后启动,倒计时的逻辑写在handler里面 privat

winfrom 倒计时控件

最近在做一个快递柜项目,要求在用户没有操作的时间到了一分钟,自动返回主页,我于是封装了一个倒计时控件,废话少说,直接上代码 public partial class RemainingTimeUC : UserControl { public RemainingTimeUC() { InitializeComponent(); } Form ParantForm = null; private void RemainingTimeUC_Load(object sender, EventArgs

android中倒计时控件CountDownTimer分析

android中倒计时控件CountDownTimer分析 1 示例代码 new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { LogUtil.i(TAG, "seconds remaining: " + millisUntilFinished / 1000); } public void onFinish() { LogUtil.i(TAG, "done!"

模仿淘宝客户端倒计时控件

在前面的文章中,我们分析了淘宝android客户端的一些界面时间和用户体验,今天这篇文章,主要介绍如何使用自定义控件,实现抢购倒计时的功能. 首先,我们看一下实现的效果. 实现效果很简单哈,就是一个倒计时的自定义控件. 下面简单介绍一下实现的思路. 首先,显示时间使用的是Textview,因为没有很特殊的效果,因此,我们可以自己写一个简单的布局文件,来作为显示的界面. 而关于时间的变更,我们使用timer类就可以实现,用一个1000毫秒的Timer,每过一秒,更新一下界面即可. 但是在更新时间的

自定义控件----倒计时控件

先上效果图: 因为时分秒都有自己的背景色等布局,所以重写一个textview 不够灵活,所以我们自定义一个TimeTextView继承自Linearlayout 然后再在里面放几个textview即可. 先看 布局文件吧: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi

倒计时时间控件

在做一些电商或购物的app时,我们经常会看到倒计时抢购的标示,今天恰有时间把项目中用到的该控件提取出来,有需要的可以参考下 ====================================================================================================== 版权所有,如需转载请标明出处:http://blog.csdn.net/you4580 =========================================

Android常用酷炫控件(开源项目)github地址汇总

转载一个很牛逼的控件收集贴... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView.ScrollView.TimeView.TipView.FlipView.ColorPickView.GraphView.UI Style 等等. 一.ListView android-pulltorefresh一个强大的拉动

采用数字控件的倒计时

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gra