打开设计窗体报错:To prevent possible data loss before loading the designer
打开设计窗体报错:To prevent possible data loss before loading the designer任何使用VS开发工具的朋友都会遇到此问题, 比如窗体的Load事件内有如下代码:
从这段代码可以看出问题吗? 编译后运行程序绝对没问题! 因为SystemConfig.CurrentConfig对象在运行程序第一时间已被实例化. 但是在设计环境下会报错:
To prevent possible data loss before loading the designer, the following errors must be resolved:
Object reference not set to an instance of an object.

为什么呢? 简直不可思议! VS开发工具在打开设计窗体时会检查或运行构造器, Load, Shown等事件内的代码! 当这些事件使用到未被实例化的对象或变量时, 错误呈现在你眼前. Delphi,VB等开发工具根本没这种情况.
无奈, 将代码改为:
或者SystemConfig.CurrentConfig对象在声明变量时实例化.

扫一扫加作者微信

C# Code:
private void frmBase_Load(object sender, EventArgs e)
{
this.DoSetFont(SystemConfig.CurrentConfig.FontSize, true);
}
private void frmBase_Load(object sender, EventArgs e)
{
this.DoSetFont(SystemConfig.CurrentConfig.FontSize, true);
}
从这段代码可以看出问题吗? 编译后运行程序绝对没问题! 因为SystemConfig.CurrentConfig对象在运行程序第一时间已被实例化. 但是在设计环境下会报错:
To prevent possible data loss before loading the designer, the following errors must be resolved:
Object reference not set to an instance of an object.
为什么呢? 简直不可思议! VS开发工具在打开设计窗体时会检查或运行构造器, Load, Shown等事件内的代码! 当这些事件使用到未被实例化的对象或变量时, 错误呈现在你眼前. Delphi,VB等开发工具根本没这种情况.
无奈, 将代码改为:
C# Code:
private void frmBase_Load(object sender, EventArgs e)
{
if (SystemConfig.CurrentConfig!=null)
this.DoSetFont(SystemConfig.CurrentConfig.FontSize, true);
}
private void frmBase_Load(object sender, EventArgs e)
{
if (SystemConfig.CurrentConfig!=null)
this.DoSetFont(SystemConfig.CurrentConfig.FontSize, true);
}
或者SystemConfig.CurrentConfig对象在声明变量时实例化.

扫一扫加作者微信


版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网