内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
看起来会是这样的:
//pseudo code: if (Application.Current.ExecutingStatus == ExecutingStatus.DesignMode) { ... }
我需要这样做的原因是:当我的应用程序在表达式混合中以设计模式显示时,我希望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); } } }
即
// 'this' is your UI element DesignerProperties.GetIsInDesignMode(this);
编辑:使用Silverlight/WP7时,应使用IsInDesignTool自GetIsInDesignMode
有时在VisualStudio中返回false:
DesignerProperties.IsInDesignTool
编辑:最后,为了保持完整性,WinRT/Metro/WindowsStore应用程序中的等效值是DesignModeEnabled:
Windows.ApplicationModel.DesignMode.DesignModeEnabled