【发布时间】:2014-10-09 15:29:50
【问题描述】:
如果经过的时间等于存储在 DateTime 对象 workDt 中的时间值,我正在尝试触发 Stopwatch.reset();,方法是使用 .equals() 将秒表的经过时间与day 存储在我的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();
}
}
这是实现此目的的正确方法还是我应该使用其他方法?
【问题讨论】:
-
我很困惑你想在这里实现什么。您正在将花费的时间与一天中的确切时间进行比较。除非你在午夜开始行动,否则两者永远不会匹配。我认为您应该重新考虑您要在这里实现的目标。
-
workDt 是一个 DateTime 变量。它的时间值是来自文本框的字符串。例如,用户在文本框中输入:00 : 00 : 07 : 000,workDt 的时间值现在将是 7 秒。我想在秒表达到例如 7 秒时停止秒表。
标签: c# datetime if-statement windows-phone-8 stopwatch