Wpf 如何检查i';m在运行时还是设计时?

Wpf 如何检查i';m在运行时还是设计时?,wpf,Wpf,我开发了一些userControl,其中包含一些关于“IsVisible”方法(override方法)的信息检查 当我在某个窗口上使用此usercontrol时,我看到一些错误,因为“IsVisible”方法查找在运行时设置的某个变量 如何检查我是否在设计阶段,系统是否未运行 谢谢你的帮助 DesignerProperties.GetIsInDesignMode(此模式)如果您在设计时,这将返回true public partial class MainWindow : Window {

我开发了一些userControl,其中包含一些关于“IsVisible”方法(override方法)的信息检查

当我在某个窗口上使用此usercontrol时,我看到一些错误,因为“IsVisible”方法查找在运行时设置的某个变量

如何检查我是否在设计阶段,系统是否未运行


谢谢你的帮助

DesignerProperties.GetIsInDesignMode(此模式)如果您在设计时,这将返回true

public partial class MainWindow : Window
{
    public MainWindow()
    {
        if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            Console.WriteLine("The main window is in design mode.");
    }
}

另一个答案在技术上是正确的,但我提供这个答案是为了澄清名称空间和用法。

这个答案有一个错误:您可能想调用DesignerProperties.GetIsInDesignMode(d),它可以在System.ComponentModel中找到。请注意,d必须是UIElement。此DesignerProperty是一个附加属性。这不是可调用的,但必须是
GetIsInDesignMode
。@ChristianIvicevic Oops,感谢您指出这一点!固定的。