태그 보관물: winforms

winforms

Control의 생성자에서 디자인 모드 감지 런타임 모드인지 감지 할

이 질문 에 이어 객체의 생성자 내에서 디자인 모드인지 런타임 모드인지 감지 할 수 있습니까?

나는 이것이 가능하지 않을 수 있으며 내가 원하는 것을 변경해야 할 것임을 알고 있지만 지금은이 특정 질문에 관심이 있습니다.



답변

네임 스페이스 에서 LicenceUsageMode 열거를 사용할 수 있습니다 System.ComponentModel.

bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);


답변

다음과 같은 것을 찾고 있습니까?

public static bool IsInDesignMode()
{
    if (Application.ExecutablePath.IndexOf("devenv.exe", StringComparison.OrdinalIgnoreCase) > -1)
    {
        return true;
    }
    return false;
}

프로세스 이름을 확인하여 수행 할 수도 있습니다.

if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
   return true;


답변

구성 요소 … 내가 아는 한 DesignMode 속성이 없습니다. 이 속성은 Control에서 제공합니다. 그러나 문제는 CustomControl이 디자이너의 Form에있을 때이 CustomControl이 런타임 모드에서 실행된다는 것입니다.

DesignMode 속성이 Form에서만 올바르게 작동한다는 것을 경험했습니다.


답변

컨트롤 (Forms, UserControls 등)은 다음 Component class을 상속합니다 bool property DesignMode.

if(DesignMode)
{
  //If in design mode
}


답변

중대한

Windows Forms 또는 WPF 사용에는 차이가 있습니다 !!

그들은 다른 디자이너를 가지고 있고 다른 수표 가 필요 합니다 . 또한 Forms와 WPF 컨트롤을 혼합하면 까다 롭습니다. (예 : Forms 창 내부의 WPF 컨트롤)

Windows Forms 만 있는 경우 다음을 사용하십시오.

Boolean isInWpfDesignerMode   = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);

당신이있는 경우 에만 WPF를 ,이 검사를 사용합니다 :

Boolean isInFormsDesignerMode = (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");

Forms와 WPF를 혼합하여 사용 하는 경우 다음 과 같은 검사를 사용하십시오.

Boolean isInWpfDesignerMode   = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
Boolean isInFormsDesignerMode = (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");

if (isInWpfDesignerMode || isInFormsDesignerMode)
{
    // is in any designer mode
}
else
{
    // not in designer mode
}

현재 모드를 보려면 디버깅을위한 MessageBox를 표시 할 수 있습니다.

// show current mode
MessageBox.Show(String.Format("DESIGNER CHECK:  WPF = {0}   Forms = {1}", isInWpfDesignerMode, isInFormsDesignerMode));

말:

System.ComponentModelSystem.Diagnostics 네임 스페이스를 추가해야합니다 .


답변

Component.DesignMode 속성을 사용해야합니다. 내가 아는 한 이것은 생성자에서 사용해서는 안됩니다.


답변

또 다른 흥미로운 방법은 해당 블로그에 설명되어 있습니다. http://www.undermyhat.org/blog/2009/07/in-depth-a-definitive-guide-to-net-user-controls-usage-mode-designmode-or -usermode /

기본적으로 항목 어셈블리에서 정적으로 참조되는 실행 어셈블리를 테스트합니다. 어셈블리 이름 ( ‘devenv.exe’, ‘monodevelop.exe’..)을 추적 할 필요가 없습니다.

그러나 어셈블리가 동적으로로드되는 다른 모든 시나리오에서는 작동하지 않습니다 (VSTO가 한 예임).