相关疑难解决方法(0)

有没有办法检查WPF当前是否在设计模式下执行?

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

它看起来像这样:

//pseudo code:
if (Application.Current.ExecutingStatus == ExecutingStatus.DesignMode) 
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

我需要这个的原因是:当我的应用程序在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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# wpf expression-blend

140
推荐指数
6
解决办法
6万
查看次数

标签 统计

c# ×1

expression-blend ×1

wpf ×1