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

有谁知道一些可用的全局状态变量,以便我可以检查代码当前是否正在设计模式下执行(例如在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);

        }

    }

}


幕布斯6054654
浏览 767回答 3
3回答

拉风的咖菲猫

我相信您正在寻找具有DependencyObject的GetIsInDesignMode。就是// 'this' is your UI elementDesignerProperties.GetIsInDesignMode(this);编辑:使用Silverlight / WP7时,应使用,IsInDesignTool因为GetIsInDesignMode有时在Visual Studio中可能返回false:DesignerProperties.IsInDesignTool编辑:最后,出于完整性考虑,WinRT / Metro / Windows Store应用程序中的等效项是DesignModeEnabled:Windows.ApplicationModel.DesignMode.DesignModeEnabled

至尊宝的传说

您可以执行以下操作:DesignerProperties.GetIsInDesignMode(new DependencyObject());

跃然一笑

public static bool InDesignMode(){&nbsp; &nbsp; return !(Application.Current is App);}可在任何地方工作。我用它来阻止数据绑定视频在设计器中播放。
打开App,查看更多内容
随时随地看视频慕课网APP