很遗憾,我发现有时我写的代码在运行时完全正常,但在使用Visual Studio 2010中的XAML/设计师时会引起麻烦。我的最爱的例子包括多个MessageBox用于调试出现,然而,当前的示例是构造函数中非常轻量级的单例式条件,这意味着当我想要在XAML中更改实例时,我必须重新构建解决方案。
是否有预处理器指令可用于跳过XAML设计器中的代码?
例:
public class CustomFE : FrameworkElement
{
public CustomFE()
{
#if !XAMLDesigner // Or something similar
if (_instance != null)
throw new NotSupportedException("Multiple instances not supported");
#endif
_instance = this;
}
private static CustomFE _instance = null;
public static CustomFE Instance
{
get { return _instance; }
}
}