Edw*_*uay 140 c# wpf expression-blend
有没有人知道一些可用的全局状态变量,以便我可以检查代码当前是否在设计模式下执行(例如在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)
Ric*_*lay 218
我相信您正在寻找GetIsInDesignMode,它采用DependencyObject.
IE浏览器.
// 'this' is your UI element
DesignerProperties.GetIsInDesignMode(this);
Run Code Online (Sandbox Code Playgroud)
编辑:使用Silverlight/WP7时,您应该使用,IsInDesignTool
因为GetIsInDesignMode
在Visual Studio中有时会返回false:
DesignerProperties.IsInDesignTool
Run Code Online (Sandbox Code Playgroud)
编辑:最后,为了完整性,WinRT/Metro/Windows Store应用程序中的等价物是DesignModeEnabled
:
Windows.ApplicationModel.DesignMode.DesignModeEnabled
Run Code Online (Sandbox Code Playgroud)
Sac*_*tin 110
你可以这样做:
DesignerProperties.GetIsInDesignMode(new DependencyObject());
Run Code Online (Sandbox Code Playgroud)
Pat*_*ick 23
public static bool InDesignMode()
{
return !(Application.Current is App);
}
Run Code Online (Sandbox Code Playgroud)
从任何地方工作.我用它来阻止数据绑定视频在设计师中播放.
当Visual Studio自动为我生成一些代码时使用它
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
...
}
Run Code Online (Sandbox Code Playgroud)
如本相关回答所述,还有其他(可能更新的)方法在WPF中指定设计时数据.
实质上,您可以使用ViewModel的设计时实例指定设计时数据:
d:DataContext="{d:DesignInstance Type=v:MySampleData, IsDesignTimeCreatable=True}"
Run Code Online (Sandbox Code Playgroud)
或者通过在XAML文件中指定示例数据:
d:DataContext="{d:DesignData Source=../DesignData/SamplePage.xaml}">
Run Code Online (Sandbox Code Playgroud)
您必须将SamplePage.xaml
文件属性设置为:
BuildAction: DesignData
Copy to Output Directory: Do not copy
Custom Tool: [DELETE ANYTHING HERE SO THE FIELD IS EMPTY]
Run Code Online (Sandbox Code Playgroud)
我将这些放在我的UserControl
标签中,如下所示:
<UserControl
...
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
...
d:DesignWidth="640" d:DesignHeight="480"
d:DataContext="...">
Run Code Online (Sandbox Code Playgroud)
在运行时,所有"d:"设计时标签都会消失,因此您只能获得运行时数据上下文,但是您可以选择设置它.
编辑 您可能还需要这些行(我不确定,但它们似乎相关):
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Run Code Online (Sandbox Code Playgroud)
如果您在大型WPF/Silverlight/WP8/WinRT应用程序中广泛使用Caliburn.Micro,您也可以在视图模型中使用方便和通用的 caliburn 静态属性(并且它在Blend中与Visual Studio一样好):Execute.InDesignMode
using Caliburn.Micro;
// ...
/// <summary>
/// Default view-model's ctor without parameters.
/// </summary>
public SomeViewModel()
{
if(Execute.InDesignMode)
{
//Add fake data for design-time only here:
//SomeStringItems = new List<string>
//{
// "Item 1",
// "Item 2",
// "Item 3"
//};
}
}
Run Code Online (Sandbox Code Playgroud)