Ian*_*Ian 8 wpf design-time mvvm expression-blend
[使用vs2010和表达式混合v4]
嗨 - 尝试在WPF和Blend中加载一些设计时数据,使用Josh Smith的概念:http://joshsmithonwpf.wordpress.com/2010/04/07/assembly-level-initialization-at-design-time/ eg
[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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的属性在AssemblyInfo.cs中,AppBootstrapper扩展了MefBootstrapper.
[assembly: AssemblyCopyright("Copyright © 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: DesignTimeBootstrapper(typeof(AppBootstrapper))]
Run Code Online (Sandbox Code Playgroud)
我不想使用Blend样本数据,a)因为它似乎没有为ObservableCollection创建数据而b)我按照定义处于设计模式,因此事情会发生很大变化,但我的'生成的数据' 将不会.
无论如何,似乎没有任何事情发生.
Q1:如何调试我的bootstrapper的设计时初始化?Q2:我的View XAML中是否需要额外的混合命名空间/属性等?
(在我的引导程序中,我只是注册了一个不同的模块,我想用DesignTimeService替换RunTimeService,导出IService接口).
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1760 次 |
| 最近记录: |