【使用vs2010&表情混合v4】
嗨 - 尝试在 WPF 和 Blend 中加载一些设计时数据,在这里使用 Josh Smith 的概念:http://joshsmithonwpf.wordpress.com/2010/04/07/assembly-level-initialization-at-design-time/
例如
[AttributeUsage(AttributeTargets.Assembly)]
public class DesignTimeBootstrapperAttribute : Attribute
{
public DesignTimeBootstrapperAttribute(Type type)
{
var dep = new DependencyObject();
Debug.WriteLine("here..?");
if (DesignerProperties.GetIsInDesignMode(dep))
{
// TODO: Design-time initialization…
IBootstrapper instance = Activator.CreateInstance(type) as IBootstrapper;
if (instance != null)
{
instance.Run();
}
}
}
}
使用我在 AssemblyInfo.cs 中的属性,其中 AppBootstrapper 扩展了 MefBootstrapper。
[assembly: AssemblyCopyright("Copyright © 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: DesignTimeBootstrapper(typeof(AppBootstrapper))]
我不想使用 Blend 示例数据,a) 因为它似乎没有为 ObservableCollection 创建数据,b) 根据定义我处于设计模式,所以事情会发生很大变化,但是我的“生成的数据” ' 将不会。
无论如何,似乎什么都没有发生。
Q1:如何调试我的 Bootstrap 的设计时初始化?
Q2:我的 View XAML 中是否需要额外的混合命名空间/属性等?
(在我的 Bootstrap 中,我只是注册了一个不同的模块,我想用 DesignTimeService 替换 RunTimeService,导出 IService 接口(interface))。
TIA
最佳答案
要调试这个:
此外,任何
Debug.WriteLine
应出现在 VS2010 输出窗口中。如果你不能让属性方法起作用(我自己没试过),你可以使用 MVVM Light 中的这个方法(我用过) :
private bool? _isInDesignMode;
public bool IsInDesignMode
{
get
{
if (!_isInDesignMode.HasValue)
{
var prop = DesignerProperties.IsInDesignModeProperty;
_isInDesignMode =
(bool)DependencyPropertyDescriptor
.FromProperty(prop, typeof(FrameworkElement))
.Metadata.DefaultValue;
}
return _isInDesignMode.Value;
}
}
关于wpf - WPF 中的设计时数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4716392/