質問
が発見した有効な解決策をDesignModeの場合の問題を開発す
問題がたくさん含まれていて巣を制御しDesignModeのみの作品のレベル。第二に、低レベルDesignModeは常にFALSEを返します。
標準ハックして、名前のプロセスの実行中であれば"DevEnv.EXE"しなければスタジオとDesignModeはTRUEです。
の問題がありますのがprocessname作品がその方法を通じて、レジストリ、その他の不思議な部品の最終結果のユーザーなどでは、必要な権の処理の名前です。またこの不思議なルートがとても遅くなります。いた矢追加hacks利用シングルトンは、エラーになる場合は、スローされたうえでのプロセス名をとDesignModeはFALSEです。
エクスペディアをDesignModeが必要なのだ。AcuallyっMicrosoft固定となりますので、することになっていても良好!
解決
の再訪問の際にこの問いに、私は現在"を発見した'5つの方法を行なうことは以下の通りである。
System.ComponentModel.DesignMode property
System.ComponentModel.LicenseManager.UsageMode property
private string ServiceString()
{
if (GetService(typeof(System.ComponentModel.Design.IDesignerHost)) != null)
return "Present";
else
return "Not present";
}
public bool IsDesignerHosted
{
get
{
Control ctrl = this;
while(ctrl != null)
{
if((ctrl.Site != null) && ctrl.Site.DesignMode)
return true;
ctrl = ctrl.Parent;
}
return false;
}
}
public static bool IsInDesignMode()
{
return System.Reflection.Assembly.GetExecutingAssembly()
.Location.Contains("VisualStudio"))
}
しているのかの解決案を作成しました少しテストソリューションとつのプロジェクト
- TestApp(サポート),
- SubControl(dll)
- SubSubControl(dll)
その後、組み込みのSubSubControlのSubControl、そのそれぞれのTestApp.ます。
この画面を表示した場合の結果に走っています。
このスクリーンショットの結果の開Visual Studio:
結論:うで 反射 この信頼 以内 コンストラクタはLicenseUsageの一つである信頼性 外部 コンストラクタが'IsDesignedHosted'( BlueRaja 下記
PS:見ToolmakerSteveのコメントは以下の(まだ試):"ご注意 IsDesignerHosted の答えとして、更にLicenseUsage...、今までに試験するだけで簡単にif(IsDesignerHosted).代替アプローチが 試験LicenseManagerにコンストラクタとキャッシュの結果."
他のヒント
から このページ:
([編集2013年] 編集の仕事をコンストラクタを使用方法を提供@hopla)
/// <summary>
/// The DesignMode property does not correctly tell you if
/// you are in design mode. IsDesignerHosted is a corrected
/// version of that property.
/// (see https://connect.microsoft.com/VisualStudio/feedback/details/553305
/// and http://stackoverflow.com/a/2693338/238419 )
/// </summary>
public bool IsDesignerHosted
{
get
{
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
return true;
Control ctrl = this;
while (ctrl != null)
{
if ((ctrl.Site != null) && ctrl.Site.DesignMode)
return true;
ctrl = ctrl.Parent;
}
return false;
}
}
私の提出 バグ報告 Microsoft;かりではないように思えますのでどこへ行くのが決まっ、これは明らかにバグの有無にかかわらず、この "デザイン").
ぜひチェックLicenseManager.UsageMode.この物件の価値LicenseUsageMode.ランタイムやLicenseUsageMode.Designtime.
がたいコードだけを走るランタイム時において、以下のコード:
if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
{
bla bla bla...
}
このメソッドを使ってい内部形態
/// <summary>
/// Gets a value indicating whether this instance is in design mode.
/// </summary>
/// <value>
/// <c>true</c> if this instance is in design mode; otherwise, <c>false</c>.
/// </value>
protected bool IsDesignMode
{
get { return DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime; }
}
この結果は正しい場合でも、いずれかのDesignModeはLicenseManager性に失敗します。
このコードは成功
public static bool IsRealDesignerMode(this Control c)
{
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
return true;
else
{
Control ctrl = c;
while (ctrl != null)
{
if (ctrl.Site != null && ctrl.Site.DesignMode)
return true;
ctrl = ctrl.Parent;
}
return System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv";
}
}
私の提案が最適@blueraja-danny-pflughoeft 返信.このソリューションな計算結果は毎回が初めてオブジェクトは変更できませんのでUsageModeデザインから実行)
private bool? m_IsDesignerHosted = null; //contains information about design mode state
/// <summary>
/// The DesignMode property does not correctly tell you if
/// you are in design mode. IsDesignerHosted is a corrected
/// version of that property.
/// (see https://connect.microsoft.com/VisualStudio/feedback/details/553305
/// and https://stackoverflow.com/a/2693338/238419 )
/// </summary>
[Browsable(false)]
public bool IsDesignerHosted
{
get
{
if (m_IsDesignerHosted.HasValue)
return m_IsDesignerHosted.Value;
else
{
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
{
m_IsDesignerHosted = true;
return true;
}
Control ctrl = this;
while (ctrl != null)
{
if ((ctrl.Site != null) && ctrl.Site.DesignMode)
{
m_IsDesignerHosted = true;
return true;
}
ctrl = ctrl.Parent;
}
m_IsDesignerHosted = false;
return false;
}
}
}
使用しLicenseManager方法、キャッシュの値からのコンストラクタでの使用全体の寿命についてのインスタンス.
public MyUserControl()
{
InitializeComponent();
m_IsInDesignMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
}
private bool m_IsInDesignMode = true;
public bool IsInDesignMode { get { return m_IsInDesignMode; } }
VBバージョン:
Sub New()
InitializeComponent()
m_IsInDesignMode = (LicenseManager.UsageMode = LicenseUsageMode.Designtime)
End Sub
Private ReadOnly m_IsInDesignMode As Boolean = True
Public ReadOnly Property IsInDesignMode As Boolean
Get
Return m_IsInDesignMode
End Get
End Property
こんに入ってこないが、思いを歩くだけでバックアップを親会社からの制御がDesignMode設定で以上ですか?
以降の手法の信頼性の高い(DesignMode,LicenseManager)効率(工程の再帰チェック)を使用してい public static bool Runtime { get; private set }
でプログラムレベルを明示的に設定することで内部の()メソッドがあります。
DesignModeは駿河湾、富士山、天城連山が一望(かい).その答えを提供する公共性と公開のDesignModeプロップ.できcascasdeにチェーンのユーザーコントロールまで運-非ユーザーの管理または管理するデザインモードになります。このようになっ....
public bool RealDesignMode()
{
if (Parent is MyBaseUserControl)
{
return (DesignMode ? true : (MyBaseUserControl) Parent.RealDesignMode;
}
return DesignMode;
}
すべてのおUserControlsからは継承しMyBaseUserControl.まきを実施インタフェース公開の"RealDeisgnMode".
ご注意このコードではありませんライブコードのカフmusings.:)
したことがなかったので実現できない。DesignMode(とを学ぶことができましたについて何かしてくださ'C#のですが...。)
こちらは反射型バージョン:(疑いがある場合がございますの性能を決designModeProperty staticフィールド)
static bool IsDesignMode(Control control)
{
PropertyInfo designModeProperty = typeof(Component).
GetProperty("DesignMode", BindingFlags.Instance | BindingFlags.NonPublic);
while (designModeProperty != null && control != null)
{
if((bool)designModeProperty.GetValue(control, null))
{
return true;
}
control = control.Parent;
}
return false;
}
があったので戦うことでこの問題で、最近、Visual Studio2017年の使用時には入れ子UserControls.私はいくつかの方法が上記の他、調整をコードまた拡張法による作品acceptablyいます。このシーケンスチェック、収納の結果に静boolean変数などのチェックが行われていほど一度だけます。のプロセスを失わせないアイテムで、コードから実行。武器agiは、dexで下がらないboxerぐ人がいる。
public static class DesignTimeHelper
{
private static bool? _isAssemblyVisualStudio;
private static bool? _isLicenseDesignTime;
private static bool? _isProcessDevEnv;
private static bool? _mIsDesignerHosted;
/// <summary>
/// Property <see cref="Form.DesignMode"/> does not correctly report if a nested <see cref="UserControl"/>
/// is in design mode. InDesignMode is a corrected that property which .
/// (see https://connect.microsoft.com/VisualStudio/feedback/details/553305
/// and https://stackoverflow.com/a/2693338/238419 )
/// </summary>
public static bool InDesignMode(
this Control userControl,
string source = null)
=> IsLicenseDesignTime
|| IsProcessDevEnv
|| IsExecutingAssemblyVisualStudio
|| IsDesignerHosted(userControl);
private static bool IsExecutingAssemblyVisualStudio
=> _isAssemblyVisualStudio
?? (_isAssemblyVisualStudio = Assembly
.GetExecutingAssembly()
.Location.Contains(value: "VisualStudio"))
.Value;
private static bool IsLicenseDesignTime
=> _isLicenseDesignTime
?? (_isLicenseDesignTime = LicenseManager.UsageMode == LicenseUsageMode.Designtime)
.Value;
private static bool IsDesignerHosted(
Control control)
{
if (_mIsDesignerHosted.HasValue)
return _mIsDesignerHosted.Value;
while (control != null)
{
if (control.Site?.DesignMode == true)
{
_mIsDesignerHosted = true;
return true;
}
control = control.Parent;
}
_mIsDesignerHosted = false;
return false;
}
private static bool IsProcessDevEnv
=> _isProcessDevEnv
?? (_isProcessDevEnv = Process.GetCurrentProcess()
.ProcessName == "devenv")
.Value;
}