DesignerProperties.IsInDesignMode和DesignerProperties.IsInDesignTool之间有什么区别?

DesignerProperties使用GetIsInDesignMode(element)IsInDesignTool公开两个类似的设计状态。

  • 它们之间有什么区别?
  • 我为什么要使用一个而不是另一个?

快速谷歌搜索显示,IsInDesignTool似乎适用于Silverlight和ExpressionBlend。 因此,也许IsInDesignMode用于VS,但其他人需要IsInDesignTool。

它们完全是一回事

IsInDesignTool
“如果元素在设计器的上下文中运行,则为true;否则为false。” IsInDesignTool的MDSDN

GetIsInDesignMode(元件)
“元素的IsInDesignMode属性值。” GetIsInDesignMode的MDSDN(元素)

然而,在每个蓝色的月亮中,Visual Studio都会失败

 DesignerProperties.GetIsInDesignMode(this) 

所以最好用

 return DesignerProperties.GetIsInDesignMode(this) || DesignerProperties.IsInDesignTool; 

如果你不想让它崩溃。

用于Silverlight的.Net Reflector

 public static bool GetIsInDesignMode(DependencyObject element) { if (element == null) { throw new ArgumentNullException("element"); } bool flag = false; if (Application.Current.RootVisual != null) { flag = (bool) Application.Current.RootVisual.GetValue(IsInDesignModeProperty); } return flag; } public static bool IsInDesignTool { get { return isInDesignTool; } [SecurityCritical] set { isInDesignTool = value; } } internal static bool InternalIsInDesignMode { [CompilerGenerated] get { return k__BackingField; } [CompilerGenerated] set { k__BackingField = value; } }