我在VS 2012中有这样的代码:
private void Form1_Load(object sender, EventArgs e)
{
if (Properties.Settings.Default["Database"] != null)
{
MessageBox.Show("We landed on spot 1");
}
else
{
MessageBox.Show("We landed on spot 2");
}
}
我很确定我搞砸了条件语法,但我希望其中一个会发生:
但这些都不会发生。我一直盯着这一个小时,我能找到的资源非常渺茫。 如果有经验的人可以解释我这里发生了什么?
修改 感谢JMK's link我发现这基本上是在Windows x64下VS调试器中弹出的wontfix错误。如果应用程序在调试器之外运行,则会触发错误。
答案 0 :(得分:3)
它默默地出错。
try
{
if (Properties.Settings.Default["Database"] != null)
{
MessageBox.Show("We landed on spot 1");
}
else
{
MessageBox.Show("We landed on spot 2");
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
回来时“找不到设置属性'数据库'”
答案 1 :(得分:0)
尝试在Properties
if (WindowsFormsApplication2.Properties.Settings.Default.Database != null)
答案 2 :(得分:0)
可靠的是抛出异常而调试器没有注意到。 对于64位Windows版本上的Windows窗体项目会发生这种情况(并且不是特定于.NET的行为,而是针对Windows的行为)。
此处有更多详情:Visual Studio does not break at exceptions in Form_Load Event
尝试按STRG + ALT + E
并选中“公共语言运行时例外”复选框“Thrown”。
现在,调试器将在Form_Load()
因为我知道我的解决方法是完全避免使用Load事件。
我的大多数表单都是对话框,因此我隐藏了ShowDialog()方法并调用了Init()函数。
public class Form1
{
public new DialogResult ShowDialog()
{
Init();
return base.ShowDialog();
}
public new DialogResult ShowDialog(IWin32Window owner)
{
Init();
return base.ShowDialog(owner);
}
public void Init()
{
// code goes here
}
}