wpf - WPF 中的设计时数据

标签 wpf mvvm expression-blend design-time

【使用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

最佳答案

要调试这个:

  • 在VS2010中打开你的项目
  • 在程序集属性构造函数中设置断点
  • 启动 Blend 4 的新实例
  • 从 VS2010 使用 Debug -> Attach to Process: 并选择 Blend
  • 切换到 Blend 并打开您的项目
  • 打开引用示例数据的 XAML 文件

  • 此外,任何 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/

    相关文章:

    model-view-controller - 使用我的 View 模型中对象的属性时,additionalViewData id不会覆盖自动生成的id的编辑器

    javascript - C# - 当网页上的 javascript 调用 window.close() 时关闭 System.Windows.Forms.WebBrowser

    wpf - 在 Blend 设计时使用 Moq

    wpf - 获取多行 wpf 文本 block 中的所有文本

    c# - 为什么 DataGridRow IsSelected 绑定(bind)不适用于 DataGrid

    c# - 行的WPF DataGrid StyleSelector

    c# - 如何使用 ExpressionMediaPlayer 控件在 Silverlight 中从 Web 服务器流式传输视频?

    wpf - MVVM- 我将如何在我的主视图模型(和其他 View 模型)和我的设置对话框之间传播设置?

    wpf - 您如何管理庞大且几乎无法维护的 XAML 文件?

    c# - c# WPF 中的文字字符太多