有没有一种方法,以检查是否WPF目前在设计模式执行与否? [英] Is there a way to check if WPF is currently executing in design mode or not?

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

问题描述

有谁知道一些全局状态变量是可用的,这样我可以检查code是在设计模式正在执行(如混合或Visual Studio)或不?

这将是这个样子:

  //伪code:
如果(Application.Current.ExecutingStatus == ExecutingStatus.DesignMode)
{
    ...
}

我需要这个的原因是:当被显示在设计模式在Ex pression混合我的申请,我想视图模型,而不是使用设计Customer类,它具有模拟数据,它的设计者可以鉴于设计模式。

然而,当应用程序被真正执行,我当然希望视图模型使用真实的客户类返回真实的数据。

目前我解决这个通过让设计师,他的作品在它之前,进入视图模型变ApplicationDevelopmentMode.Executing到ApplicationDevelopmentMode.Designing

 公共CustomersViewModel()
{
    _currentApplicationDevelopmentMode = ApplicationDevelopmentMode.Designing;
}公众的ObservableCollection<客户>得到所有
{
    得到
    {
        尝试
        {
            如果(_currentApplicationDevelopmentMode == ApplicationDevelopmentMode.Developing)
            {
                返回Customer.GetAll;
            }
            其他
            {
                返回CustomerDesign.GetAll;
            }
        }
        赶上(异常前)
        {
            抛出新的异常(ex.Message);
        }
    }
}


解决方案

我相信你正在寻找<一href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.designerproperties.getisindesignmode.aspx\">GetIsInDesignMode,这需要一个DependencyObject的。

  //'这'是你的UI元素
DesignerProperties.GetIsInDesignMode(本);

编辑::当使用Silverlight / WP7,你应该使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.designerproperties.isindesigntool%28v=VS.95%29.aspx\"><$c$c>IsInDesignTool因为 GetIsInDesignMode 有时会返回在Visual Studio假,而

  DesignerProperties.IsInDesignTool

编辑:最后,在完全的利益,在WinRT中的当量/城域/ Windows应用商店的应用程序是<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.designmode.designmodeenabled.aspx\"><$c$c>DesignModeEnabled:

  Windows.ApplicationModel.DesignMode.DesignModeEnabled

Does anyone know of some global state variable that is available so that I can check if the code is currently executing in design mode (e.g. in Blend or Visual Studio) or not?

It would look something like this:

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

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.

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

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);
        }
    }
}

解决方案

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

Ie.

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

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

DesignerProperties.IsInDesignTool

Edit: 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天全站免登陆