C# – 중첩된 사용자 정의 컨트롤에서의 DesignMode 문제점

UserControl로 만들고 Visual Studio의 디자인 모드에서
Form에 추가하려고 하면..
생성자 및 Load 이벤트까지 발생을 한다.
따라서 디자인 모드에서 수행을 원치 않으면 아래처럼 코딩하곤 했다..

private void ParamEditor_Load(object sender, EventArgs e)
{
    if (!this.DesignMode)
    {
        m_notifier = (INotifier)SpringContextManager.ApplicationContext.GetObject("OmcNotifier");
        m_logger = (ILog)SpringContextManager.ApplicationContext.GetObject("OmcLogger");
    }
}

 

근데..
A라는 UserControl 안에 B라는 UserControl을 넣으면
A의 DesignMode는 true, B의 DesignMode는 false가 된다.
참조 : http://support.microsoft.com/kb/839202

그래서 아래와 같이 실행중인지 체크하는 메소드를 추가하여 해결..

private void ParamEditor_Load(object sender, EventArgs e)
{
    if (!IsInDesigner)
    {
        m_notifier = (INotifier)SpringContextManager.ApplicationContext.GetObject("OmcNotifier");
        m_logger = (ILog)SpringContextManager.ApplicationContext.GetObject("OmcLogger");
    }
}

protected static bool IsInDesigner
{
    get { return (System.Reflection.Assembly.GetEntryAssembly() == null); }
}

 

댓글 남기기