最近改了个用户控件,在设计界面加载时总是报错。
网上查了下可以用DesignMode解决,但是在构造函数里用DesignMode还是不行。查原因后发现该Design模式不能嵌套UserControl,而我们继承的是XtraUserControl,所以等于嵌套了,网上查了下文章,终于找到个解决办法,代码如下:
/// <summary>
/// 设计模式,防止设计模式时报错
/// </summary>
public static bool DesignMode = true;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//使得主程序只能加载一次
if (RepeatLogin()) return;
#region 异常捕获
System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#endregion
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
#region 设置默认字体
DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font("Tahoma", 9);
#endregion
//改变设计模式
DesignMode = false;
//启动主进程
StartMainProcess();
DesignMode = true;
}
/// 设计模式,防止设计模式时报错
/// </summary>
public static bool DesignMode = true;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//使得主程序只能加载一次
if (RepeatLogin()) return;
#region 异常捕获
System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#endregion
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
#region 设置默认字体
DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font("Tahoma", 9);
#endregion
//改变设计模式
DesignMode = false;
//启动主进程
StartMainProcess();
DesignMode = true;
}
这时在UserControl里就可以用这个变量了,代码如下:
/// <summary>
/// 构造函数
/// </summary>
public TreeZDComboBox()
{
InitializeComponent();
if (!Program.DesignMode)
{
_Proxy = new KeHuGXService.KeHuGXClient();
_Proxy.GetCeBenXXBySTCompleted += new EventHandler<KeHuGXService.GetCeBenXXBySTCompletedEventArgs>(_Proxy_GetCeBenXXBySTCompleted);
}
}
/// 构造函数
/// </summary>
public TreeZDComboBox()
{
InitializeComponent();
if (!Program.DesignMode)
{
_Proxy = new KeHuGXService.KeHuGXClient();
_Proxy.GetCeBenXXBySTCompleted += new EventHandler<KeHuGXService.GetCeBenXXBySTCompletedEventArgs>(_Proxy_GetCeBenXXBySTCompleted);
}
}