WPF 中的设计时数据

发布于 2024-10-12 04:12:16 字数 1443 浏览 4 评论 0原文

[使用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) 根据定义,我处于设计模式,所以事情会发生很大变化,但我的“生成的数据” '不会。

无论如何,似乎什么也没有发生。

问题 1:如何调试引导程序的设计时初始化? 问题 2:我的视图 XAML 中是否需要额外的混合命名空间/属性等?

(在我的引导程序中,我只是注册一个不同的模块,我想用 DesignTimeService 替换 RunTimeService,导出 IService 接口)。

TIA

[using vs2010 & expression blend v4]

Hi - trying to load up some design time data in WPF and Blend, using Josh Smith's concept here: http://joshsmithonwpf.wordpress.com/2010/04/07/assembly-level-initialization-at-design-time/
e.g.

[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();
            }
        }
    }
}

With my attribute here in AssemblyInfo.cs, where AppBootstrapper extends MefBootstrapper.

[assembly: AssemblyCopyright("Copyright ©  2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: DesignTimeBootstrapper(typeof(AppBootstrapper))]

I don't want to use the Blend sample data, a) as it doesn't seem to create data for ObservableCollection and b) I'm in design mode by definition, so things will change quite a lot, but my 'generated data' will not.

Anyway, nothing seems to be happening.

Q1: How is it possible to debug the design time initialisation of my bootstrapper?
Q2: Do I need additional blend namespaces/ attributes etc in my View XAML?

(In my bootstrapper I'm just registering a different module where I want to replace RunTimeService with a DesignTimeService, exporting the IService interface).

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉服软 2024-10-19 04:12:16

要调试它:

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

此外,任何 Debug.WriteLine 都应该出现在 VS2010 输出窗口中。

如果你无法让属性方法起作用(我自己没有尝试过),你可以使用 MVVM 轻量级

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;
    }
}

To debug this:

  • Open your project in VS2010
  • Set a breakpoint in the assembly attribute constructor
  • Start a new instance of Blend 4
  • From VS2010 use Debug -> Attach to Process: and choose Blend
  • Switch to Blend and open your project
  • Open a XAML file that references your sample data

Also, any Debug.WriteLine should appear in the VS2010 output window.

If you can't get the attribute method to work (I haven't tried it myself), you can use this method (which I have used) from 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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文