如何为秒表创建一个具有暂停和播放功能的按钮 [英] How to create a button with both a pause and play function for a stopwatch

查看:102
本文介绍了如何为秒表创建一个具有暂停和播放功能的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用coding4fun工具包中的秒表代码,我正在试图弄清楚如何将暂停和播放按钮基本合并在一起,这样我就可以使用相同的按钮暂停并播放倒数计时器。我当前的代码是

 private void TimerBegin_Click(object sender,RoutedEventArgs e)
{
if(this.dispatcherTimer == null)
{
this.dispatcherTimer = new DispatcherTimer();
this.dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
this.dispatcherTimer.Tick + = new EventHandler(dispatcherTimer_Tick);
}

if(this.EndTime == DateTime.MinValue)
{
this.EndTime = DateTime.Now +(TimeSpan)this.TimeSpanPicker.Value;
}

this.dispatcherTimer.Start();

if(this.dispatcherTimer == Start)
{}


}


和暂停按钮就是这样:

 private void Pause_Click(object sender,RoutedEventArgs e)
{
this.dispatcherTimer.Stop();
}


我希望能够在一个按钮内完成这两项工作。我对c#很新,所以我不太了解。



解决方案

这很容易......


您可以测试计时器是否已停止并启动它,否则启动它。


有意义吗?


请参阅计时器的isEnabled属性中的备注:


http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.isenabled(v = vs.110)的.aspx


I'm currently using the stopwatch code from the coding4fun toolkit, and I'm trying to figure out how to essentially merge the pause and play button together so I can have the same button both pause and play the countdown timer. My current code is this :

  private void TimerBegin_Click(object sender, RoutedEventArgs e)
        {
            if (this.dispatcherTimer == null)
            {
                this.dispatcherTimer = new DispatcherTimer();
                this.dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
                this.dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            }

            if (this.EndTime == DateTime.MinValue)
            {
                this.EndTime = DateTime.Now + (TimeSpan)this.TimeSpanPicker.Value;
            }
            
            this.dispatcherTimer.Start();
        
            if (this.dispatcherTimer == Start)
            { }

            
        }

and the pause button is just this:

   private void Pause_Click(object sender, RoutedEventArgs e)
        {
            this.dispatcherTimer.Stop();
        }

I would just like to be able to do both within one button. I'm pretty new to c# so I don't know too much.

解决方案

This would be pretty easy...

You could test to see if the timer is stopped and start it if it is, otherwise start it.

Make sense?

See the remarks in the isEnabled property of the timer:

http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.isenabled(v=vs.110).aspx


这篇关于如何为秒表创建一个具有暂停和播放功能的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆