[英]DateTime, an exception of type 'System.InvalidOperationException'
Haveing this problem with a TimePicker that i am using. 使用我正在使用的TimePicker来解决这个问题。 When i dont have selected any time I am getting this error:
当我没有选择任何时候我收到此错误:
An exception of type 'System.InvalidOperationException'
Where the exception occurs is on this line: 发生异常的地方是这一行:
DateTime break1S = (DateTime)startBreak1.Value;
The problem is that it sets a default value when you dont set any value in the picker. 问题是当你没有在选择器中设置任何值时它设置一个默认值。 But why wont it use it?
但为什么不使用呢? Here is how the local variables look like:
以下是局部变量的外观:
You can see that break1S have a value so what is the problem. 你可以看到break1S有一个值,所以问题是什么。
Here is the whole code: 这是整个代码:
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);
Well, You should check, what is in "startBreak1" item during that moment. 好吧,你应该检查那一刻“startBreak1”项目中的内容。 Possibly a cast from startBreak1.Value to DateTime caused this error.
可能是从startBreak1.Value到DateTime的强制转换导致此错误。
Break1S has a value, but this has nothing to do with the problem, as ir is January 1st 0001 - an initial value for any DateTime structure. Break1S有一个值,但这与问题无关,因为ir是1月1日0001 - 任何DateTime结构的初始值。 The problem is most probably because of the cast (if startBreak1.Value is DateTime? or Nullable, then Your choise is to use another type of construct instead, ie
问题很可能是因为强制转换(如果startBreak1.Value是DateTime?或Nullable,那么你的选择是使用另一种类型的构造,即
DateTime break1S = startBreak1.Value.HasValue ? startBreak1.Value.Value : DateTime.MinValue;
Note that instead of DateTime.MinValue You can use DateTime.Now, DateTime.Today, DateTime.Now.AddMinutes(-10) or anything You think should be appropriate Default if startBreak1.Value has no actual value set. 请注意,而不是DateTime.MinValue您可以使用DateTime.Now,DateTime.Today,DateTime.Now.AddMinutes(-10)或您认为应该适当的任何内容默认情况下,如果startBreak1.Value没有设置实际值。 )
)
I am not an expert of Windows Phone, but it seems to me that the Value property of a TimePicker control is defined as DateTime? 我不是Windows Phone的专家,但在我看来,TimePicker控件的Value属性被定义为DateTime? meaning a nullable DateTime.
意思是可以为空的DateTime。
When you try to assign it to a DateTime variable (that cannot accept null values) and the TimePicker is null you get the InvalidOperationException 当您尝试将其分配给DateTime变量(不能接受空值)并且TimePicker为null时,您将获得InvalidOperationException
You could try with 你可以试试
startBreak1 = startBreak1.Value ?? DateTime.MinValue;
Check 'MinDate' and 'MaxDate' properties of your DateTimePicker. 检查DateTimePicker的“MinDate”和“MaxDate”属性。
'1/1/0001 12:00:00 AM' might not be falling between them. '1/1/0001 12:00:00 AM'可能不会落在他们之间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.