C# mscorlib.ni.dll 中发生“System.InvalidOperationException”类型的异常但未处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16893855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:04:17  来源:igfitidea点击:

Exception of type 'System.InvalidOperationException' occurred in mscorlib.ni.dll but was not handled

c#datetimewindows-phone-8timepicker

提问by mogren3000

I am getting this message:

我收到这条消息:

"Exception of type 'System.InvalidOperationException' occurred in mscorlib.ni.dll but was not handled in user code"

mscorlib.ni.dll 中发生了“System.InvalidOperationException”类型的异常,但未在用户代码中处理

This problem occurs when my TimePicker dont have any time choosen when I leave it empty.

当我将 TimePicker 留空时没有选择任何时间时,就会出现此问题。

My code for the TimePicker looks like this;

我的 TimePicker 代码如下所示;

DateTime break1S = (DateTime)startBreak1.Value; 

Its on this row i am getting the message but if i set a time for the picker i dont get an message.

在这一行,我收到了消息,但如果我为选择器设置了时间,我就不会收到消息。

Any ideas?

有任何想法吗?

**

**

采纳答案by Rapha?l Althaus

If startBreak1.Valueis a string :

如果startBreak1.Value是字符串:

if (startBreak1!= null) 
    DateTime.TryParse(startBreak1.Value, out break1S);

if it's a Nullable<DateTime>(and I think it is)

如果是Nullable<DateTime>(我认为是)

DateTime break1S = startBreak1.HasValue 
                          ? startBreak1.Value
                          : new DateTime()//or anything you want;

or accept that break1Scan be nullable :

或接受break1S可以为空:

var break1S = startBreak1;

回答by mogren3000

The solution look like this:?

解决方案如下:?

DateTime break1S = startBreak1.Value.HasValue ? startBreak1.Value.Value : DateTime.MinValue;

回答by user2470005

you can try this:

你可以试试这个:

DateTime break1S = startBreak1.Value.HasValue ? startBreak1.Value.Value : DateTime.MinValue;