有没有办法检查 WPF 当前是否在设计模式下执行? [英] Is there a way to check if WPF is currently executing in design mode or not?

查看:31
本文介绍了有没有办法检查 WPF 当前是否在设计模式下执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道一些可用的全局状态变量,以便我可以检查代码当前是否在设计模式下(例如在 Blend 或 Visual Studio 中)执行?

它看起来像这样:

//pseudo code:
if (Application.Current.ExecutingStatus == ExecutingStatus.DesignMode) 
{
    ...
}

我需要这个的原因是:当我的应用程序在 Expression Blend 中以设计模式显示时,我希望 ViewModel 使用设计客户类",其中包含设计人员可以在设计模式下查看的模拟数据.

The reason I need this is: when my application is being shown in design mode in Expression Blend, I want the ViewModel to instead use a "Design Customer class" which has mock data in it that the designer can view in design mode.

但是,当应用程序实际执行时,我当然希望 ViewModel 使用返回真实数据的真实 Customer 类.

However, when the application is actually executing, I of course want the ViewModel to use the real Customer class which returns real data.

目前我通过让设计师解决这个问题,在他开始工作之前,进入 ViewModel 并将ApplicationDevelopmentMode.Executing"更改为ApplicationDevelopmentMode.Designing":

Currently I solve this by having the designer, before he works on it, go into the ViewModel and change "ApplicationDevelopmentMode.Executing" to "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);
        }
    }
}

推荐答案

我相信您正在寻找 GetIsInDesignMode,它需要一个 DependencyObject.

I believe you are looking for GetIsInDesignMode, which takes a DependencyObject.

// 'this' is your UI element
DesignerProperties.GetIsInDesignMode(this);

在使用 Silverlight/WP7 时,您应该使用 IsInDesignTool 因为 GetIsInDesignMode 在 Visual Studio 中有时会返回 false:

When using Silverlight / WP7, you should use IsInDesignTool since GetIsInDesignMode can sometimes return false while in Visual Studio:

DesignerProperties.IsInDesignTool

最后,为了完整起见,WinRT/Metro/Windows Store 应用程序中的等效项是 DesignModeEnabled:

And finally, in the interest of completeness, the equivalent in WinRT / Metro / Windows Store applications is DesignModeEnabled:

Windows.ApplicationModel.DesignMode.DesignModeEnabled

这篇关于有没有办法检查 WPF 当前是否在设计模式下执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆