我正在使用的 TimePicker 有这个问题。当我没有选择任何时间时,我收到此错误:
An exception of type 'System.InvalidOperationException'
发生异常的地方在这一行:
DateTime break1S = (DateTime)startBreak1.Value;
问题在于,当您未在选择器中设置任何值时,它会设置默认值。但它为什么不使用它呢?以下是局部变量的样子:
您可以看到 break1S 有一个值,那么问题出在哪里。
这是整个代码:
DateTime date = (DateTime)datePicker.Value;
DateTime start = (DateTime)startingTime.Value;
DateTime end = (DateTime)endingTime.Value;
DateTime break1S = (DateTime)startBreak1.Value;
DateTime break1E = (DateTime)endBreak1.Value;
DateTime break1S = (DateTime)startBreak1.Value;
DateTime break1E = (DateTime)endBreak1.Value;
DateTime break2S = (DateTime)startBreak2.Value;
DateTime break2E = (DateTime)endBreak2.Value;
_nestedDateStart = new DateTime(date.Year, date.Month, date.Day, start.Hour, start.Minute, 0);
_nestedDateEnd = new DateTime(date.Year, date.Month, date.Day, end.Hour, end.Minute, 0);
_nestedDateStartBreak1 = new DateTime(date.Year, date.Month, date.Day, break1S.Hour, break1S.Minute, 0);
_nestedDateEndBreak1 = new DateTime(date.Year, date.Month, date.Day, break1E.Hour, break1E.Minute, 0);