【发布时间】:2014-11-28 10:37:10
【问题描述】:
我已经将文本框中的字符串值分配给日期时间变量。它的目的是作为一个标志来告诉 myTimer 对象在达到存储在 workDt 中的时间时停止,(日期时间变量)。
我尝试过的当前实现如下,我设置了一个 if..else 语句来检查计时器的当前时间是否等于在文本框中输入的时间,但它不会触发计时器停止.
我在“if”语句上设置了一个断点,时间值存储在 workDt 中,但没有触发计时器停止。
任何人都可以看到我的实施中的缺陷或就替代解决方案提供任何建议吗?
private void startBtn_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
string wrkString;
string rstString;
//Assign text box time string to string variables.
wrkString = wrkTbx.Text;
rstString = restTbx.Text;
//Assign text box string value to a date time variable.
DateTime workDt = DateTime.ParseExact(wrkString.Replace(": ", ":").Replace(" :", ":"), "HH:mm:ss:fff", CultureInfo.InvariantCulture);
DateTime restDt = DateTime.ParseExact(rstString.Replace(": ", ":").Replace(" :", ":"), "HH:mm:ss:fff", CultureInfo.InvariantCulture);
StopGoCvs.Background = new SolidColorBrush(Colors.Green);
hornElmt.Play();
// // set up the timer
myTimer = new DispatcherTimer();
myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
myTimer.Tick += myTimer_Tick;
//tell timer to stop when it has reached the allocated work time.
if(myTimer.Interval != workDt.TimeOfDay)
{
// start both timers
myTimer.Start();
myStopwatch.Start();
}
else
{
myTimer.Stop();
myStopwatch.Stop();
}
}
【问题讨论】:
标签: c# datetime windows-phone-8 timer stopwatch