Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF中是否有DesignMode属性?_Wpf_.net 3.5 - Fatal编程技术网

WPF中是否有DesignMode属性?

WPF中是否有DesignMode属性?,wpf,.net-3.5,Wpf,.net 3.5,用Winforms你可以说 if ( DesignMode ) { // Do something that only happens on Design mode } WPF中有这样的东西吗?确实有: 例如: using System.ComponentModel; using System.Windows; using System.Windows.Controls; public class MyUserControl : UserControl { public MyU

用Winforms你可以说

if ( DesignMode )
{
  // Do something that only happens on Design mode
}

WPF中有这样的东西吗?

确实有:

例如:

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

public class MyUserControl : UserControl
{
    public MyUserControl()
    {
        if (DesignerProperties.GetIsInDesignMode(this))
        {
            // Design-mode specific functionality
        }
    }
}

在某些情况下,我需要知道对我的非UI类的调用是否由设计器发起(例如,如果我从XAML创建DataContext类)。那么这种方法是有帮助的:

// Check for design mode. 
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)) 
{
    //in Design mode
}

对于WinForms中托管的任何WPF控件
DesignerProperties.GetIsInDesignMode(此)
不起作用

因此,我创建并添加了一个变通方法:

public static bool IsInDesignMode()
{
    if ( System.Reflection.Assembly.GetExecutingAssembly().Location.Contains( "VisualStudio" ) )
    {
        return true;
    }
    return false;
}
使用这个:

if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
{
    //design only code here
}
(异步和文件操作在这里不起作用)

另外,在XAML中实例化设计时对象(d是特殊的设计器命名空间)


...

我知道答案迟了-但是对于任何想在
数据触发器中使用此选项的人,或者XAML中的任何地方:

xmlns:componentModel=“clr命名空间:System.componentModel;assembly=PresentationFramework”

请注意,GetIsInDesignMode受到我在应用程序中应用了您的解决方案的影响,但它不起作用。我在这里问的。如果愿意,请加入我们讨论。@serhio感谢您指出这一点。你知道有什么解决办法吗?顺便说一句,它似乎在Silverlight中也不起作用:在VS2019中,必须启用开关
启用项目代码
(或者菜单->设计->我在我的应用程序中应用了您的解决方案,但它不起作用。我在这里问过。如果您愿意,请加入我们并讨论。该类(
Windows.ApplicationModel
)用于商店应用程序,包含在Windows运行时API中。如果您只是在常规Windows桌面应用程序上工作,这不是现成的WPF解决方案。它不应该是
GetEntryAssembly()
而不是
getExecutionGassembly()
?后者应该返回定义此属性的程序集
<Grid d:DataContext="{d:DesignInstance Type=local:MyViewModel, IsDesignTimeCreatable=True}">
...
</Grid>