C#预处理器 - 禁用XAML Designer的代码

时间:2011-05-20 13:38:26

标签: c# wpf silverlight visual-studio-2010 xaml

不幸的是,我发现有时我编写的代码虽然在运行时非常好,但在使用Visual Studio 2010中的XAML / Designer时会让我感到头痛。我最喜欢的例子包括多个用于调试显示的MessageBoxes,但是,当前示例在构造函数中是一个非常轻的Singleton样式条件,这意味着当我想在XAML中对实例进行更改时,我必须重建解决方案。

我是否可以使用预处理程序指令跳过XAML Designer中的代码?

示例:

    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; }
        }
    }

1 个答案:

答案 0 :(得分:4)

您可以使用DesignerProperties.GetIsInDesignMode方法,如下所示:

if (!DesignerProperties.GetIsInDesignMode(this) && _instance != null)
    throw new NotSupportedException(...)
相关问题