如何防止Designer在UserControl的Load事件中执行代码。 [英] How to prevent Designer from executing code in a UserControl's Load event.

查看:79
本文介绍了如何防止Designer在UserControl的Load事件中执行代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个带有表单Form1和UserControl UserControlA的WinForms应用程序。



如果将UserControlA拖放到Form1上在VS2005 Designer中,每次在Designer中打开Form1时,Designer都会执行UserControlA的Load事件处理程序。



这可以非常不受欢迎,例如,Load事件处理程序可能包含连接到数据库等的应用程序代码,这些代码不应在设计时执行。



在上面的场景中,解决方法是在Load事件处理程序中测试DesignMode属性,如果DesignMode为true,则跳过不需要的处理。



但是,此解决方法不适用于嵌套的UserControl。



考虑一个表单为Form1的项目,以及两个UserControls UserControlA和UserControlB。将UserControlB拖放到UserControlA,将UserControlA拖放到Form1。然后在VS设计器中构建然后打开Form1。



在这种情况下,UserControlB的Load事件由设计器执行,并且DesignMode属性已设置假设。



例如,如果UserControlB的Load事件处理程序包含以下代码,则会显示以下MessageBox:




private
void UserControlB_Load( object sender, EventArgs e)
{
if (! this .DesignMode)
{
MessageBox < font size = 2> .Show(
"在UserControlB中加载 event \ n" + 环境 .StackTrace);
}
}





有没有在嵌入UserControls的一般情况下,防止Load事件处理程序中的代码被设计者执行的方法?



谢谢,


Joe



在VS设计器中打开Form1时,上面代码显示的MessageBox:


---------------------------


---- -----------------------
在UserControlB中加载事件
在System.Environment.GetStackTrace(例外e,布尔需要文件信息)

在TestUs的System.Environment.get_StackTrace()



在System.Windows.Forms.UserControl.OnLoad中的C:\ TestSserUserControlDesigner \ TestUserControlDesigner \UserControlB.cs:第22行


中的erControlDesigner.UserControlB.UserControlB_Load(Object sender,EventArgs e) (EventArgs e)


System.Windows.Forms.UserControl.OnCreateControl()


at System.Windows.Forms.Control.CreateControl(Boolean System.Windows.Forms.Control.CreateControl()上的System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)


中的


在System.Windows.Forms.Form.ControlCollection.Add上的System.Windows.Forms.Control.ControlCollection.Add(控制值)


System.RuntimeMethodHandle._InvokeMethodFast上的System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)


中的(控制值)


(Object target,Object []参数,SignatureStruct& System.RuntimeMethodHandle.InvokeMethodFast(Object target,Object []参数,Signature sig,MethodAttributes methodAttributes,RuntimeTypeHandle typeOwner)中的sig,MethodAttributes methodAttributes,RuntimeTypeHandle typeOwner)



at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object [] parameters,CultureInfo culture,Boolean skipVisibilityChecks)


at System.Reflection.RuntimeMethodInfo .Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)


at System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target, System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression中的Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture,String [] namedParams)


(IDesignerSerializationManager管理器,字符串名称,CodeExpression表达式) )

在System.ComponentModel.Design.Serialization.CodeDomSerializer.DeserializeStatementToInstance的System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager,CodeStatement statement)



系统中的System.ComponentModel.Design.Serialization.CodeDomSerializer.Deserialize(IDesignerSerializationManager manager,Object codeObject)


中的IDesignerSerializationManager管理器,CodeStatement语句)


。 System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.DeserializeName(IDesignerSerializationManager manager,String name,CodeStatementCollection statements)中的Windows.Forms.Design.ControlCodeDomSerializer.Deserialize(IDesignerSerializationManager manager,Object codeObject)



在System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager,CodeTypeDeclaration declaration)


at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)


at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)


at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)
------ ---------------------
好的
---------------------- -----

解决方案


public partial class UserControl1:UserControl
{
public UserControl1()< br> {
InitializeComponent();
}

void void LoadUserControl_Load(object sender,EventArgs e)
{
if(Site!= null&& Site.DesignMode)return;
MessageBox.Show(" Test");
}
}


Consider a WinForms application with a form Form1 and a UserControl UserControlA.

 

If you drag and drop UserControlA on to Form1 in the VS2005 Designer, then the Designer will execute UserControlA's Load event handler(s) each time Form1 is opened in the Designer.

 

This can be highly undesirable, for example a Load event handler may contain application code to connect to a database etc that should not be executed at design time.

 

In the above scenario, a workaround is to test the DesignMode property in the Load event handler, and skip unwanted processing if DesignMode is true.

 

However, this workaround does not work for nested UserControls.

 

Consider a project with a form Form1, and two UserControls UserControlA and UserControlB.  Drag and drop UserControlB on to UserControlA, and UserControlA on to Form1.  Build then open Form1 in the VS designer.

 

In this case, UserControlB's Load event is executed by the designer, and the DesignMode property is set to false.

 

For example, if UserControlB's Load event handler contains the following code, the MessageBox below is displayed:


private void UserControlB_Load(object sender, EventArgs e)
{
if (!this.DesignMode)
{
MessageBox.Show("In UserControlB Load event\n" + Environment.StackTrace);
}
}


 

 

Is there any way to prevent code in the Load event handler from being executed by the designer in this general case where UserControls are nested?

 

Thanks,

Joe

 

MessageBox displayed by the above code when Form1 is opened in the VS designer:

---------------------------

---------------------------
In UserControlB Load event
   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)

   at System.Environment.get_StackTrace()

   at TestUserControlDesigner.UserControlB.UserControlB_Load(Object sender, EventArgs e) in C:\TestUserControlDesigner\TestUserControlDesigner\UserControlB.cs:line 22

   at System.Windows.Forms.UserControl.OnLoad(EventArgs e)

   at System.Windows.Forms.UserControl.OnCreateControl()

   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

   at System.Windows.Forms.Control.CreateControl()

   at System.Windows.Forms.Control.ControlCollection.Add(Control value)

   at System.Windows.Forms.Form.ControlCollection.Add(Control value)

   at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)

   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)

   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)

   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)

   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)

   at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)

   at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)

   at System.ComponentModel.Design.Serialization.CodeDomSerializer.DeserializeStatementToInstance(IDesignerSerializationManager manager, CodeStatement statement)

   at System.ComponentModel.Design.Serialization.CodeDomSerializer.Deserialize(IDesignerSerializationManager manager, Object codeObject)

   at System.Windows.Forms.Design.ControlCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, Object codeObject)

   at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.DeserializeName(IDesignerSerializationManager manager, String name, CodeStatementCollection statements)

   at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)

   at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)

   at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)

   at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)
---------------------------
OK  
---------------------------

解决方案


    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void LoadUserControl_Load(object sender, EventArgs e)
        {
            if (Site != null && Site.DesignMode) return;
            MessageBox.Show("Test");
        }
    }


这篇关于如何防止Designer在UserControl的Load事件中执行代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆