为了账号安全,请及时绑定邮箱和手机立即绑定

有没有一种方法可以检查WPF当前是否在设计模式下执行?

有没有一种方法可以检查WPF当前是否在设计模式下执行?

C#
幕布斯6054654 2019-10-15 14:48:58
有谁知道一些可用的全局状态变量,以便我可以检查代码当前是否正在设计模式下执行(例如在Blend或Visual Studio中)?它看起来像这样://pseudo code:if (Application.Current.ExecutingStatus == ExecutingStatus.DesignMode) {    ...}我需要这样做的原因是:当我的应用程序在Expression Blend中以设计模式显示时,我希望ViewModel改为使用其中包含模拟数据的“ Design Customer类”,设计人员可以在设计模式下进行查看。但是,当应用程序实际执行时,我当然希望ViewModel使用返回真实数据的真实Customer类。目前,我可以通过让设计者着手解决这个问题,在他进行设计之前,先进入ViewModel并将“ ApplicationDevelopmentMode.Executing”更改为“ ApplicationDevelopmentMode.Designing”:public CustomersViewModel(){    _currentApplicationDevelopmentMode = ApplicationDevelopmentMode.Designing;}public ObservableCollection<Customer> GetAll{    get    {        try        {            if (_currentApplicationDevelopmentMode == ApplicationDevelopmentMode.Developing)            {                return Customer.GetAll;            }            else            {                return CustomerDesign.GetAll;            }        }        catch (Exception ex)        {            throw new Exception(ex.Message);        }    }}
查看完整描述

3 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

我相信您正在寻找具有DependencyObject的GetIsInDesignMode。


就是


// 'this' is your UI element

DesignerProperties.GetIsInDesignMode(this);

编辑:使用Silverlight / WP7时,应使用,IsInDesignTool因为GetIsInDesignMode有时在Visual Studio中可能返回false:


DesignerProperties.IsInDesignTool

编辑:最后,出于完整性考虑,WinRT / Metro / Windows Store应用程序中的等效项是DesignModeEnabled:


Windows.ApplicationModel.DesignMode.DesignModeEnabled


查看完整回答
反对 回复 2019-10-15
?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

您可以执行以下操作:


DesignerProperties.GetIsInDesignMode(new DependencyObject());


查看完整回答
反对 回复 2019-10-15
?
跃然一笑

TA贡献1826条经验 获得超6个赞

public static bool InDesignMode()

{

    return !(Application.Current is App);

}

可在任何地方工作。我用它来阻止数据绑定视频在设计器中播放。


查看完整回答
反对 回复 2019-10-15
  • 3 回答
  • 0 关注
  • 764 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信