我试图触发Stopwatch.reset();
,如果经过的时间等于存储在DateTime对象workDt
中的时间值,可以使用.equals()将秒表经过的时间与时间进行比较存储在我的DateTime
对象中workDt.
日期时间和秒表值比较
基本上,onTapped事件会触发我的秒表以使用当前流逝的时间启动并更新文本块。
我已经将用户输入的时间值"00 : 00 : 07 : 000"
传递给我打算用于比较的DateTime对象。问题是我的条件从未满足触发myStopwatch.reset();
在测试中,我设置字符串如下,然后我在workDt上设置一个断点,它显示正在存储的正确时间,但它不会触发条件。
private async void startBtn_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//delay stop watch start to allow time to get ready.
TimeSpan time = TimeSpan.FromSeconds(1);
await System.Threading.Tasks.Task.Delay(time);
string wrkString;
string rstString;
int i;
//set text box editing to false to prevent illegal input.
wrkTbx.IsEnabled = false;
restTbx.IsEnabled = false;
roundSlider.IsEnabled = false;
roundsTbx.IsEnabled = false;
//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);
//Run the timer for until i reaches the number of rounds value.Eg, 4 rounds
//for (i = 0; i <= roundSlider.Value; i++)
//{
StopGoCvs.Background = new SolidColorBrush(Colors.Green);
//startSoundElmt.Play();
// set up the timer
myTimer = new DispatcherTimer();
myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
myTimer.Tick += myTimer_Tick;
// start both timers
myTimer.Start();
myStopwatch.Start();
//reset timer if date time value is equal to current elapsed time.
if(myStopwatch.Elapsed.Equals(workDt.TimeOfDay))
{
myStopwatch.Reset();
}
}
这是实现这一目标的正确方法还是应该使用不同的方法?
我很困惑你想在这里实现什么。您将比较所花的时间与一天中的确切时间进行比较。除非你正好在午夜开始行动,否则两人将永远不会匹配。我认为你应该重新考虑你想在这里实现的目标。 – whoisj 2014-10-09 15:33:35
workDt是一个DateTime变量。它的时间值是来自文本框的字符串。例如,用户在文本框中输入:00:00:07:000,workDt的时间值现在为7秒。例如,当它达到7秒钟时,我想停止秒表。 – 2014-10-09 15:44:23