【问题标题】:Is it possible to specify a dependency injection container for Visual Studio designer是否可以为 Visual Studio 设计器指定依赖注入容器
【发布时间】:2021-11-04 08:05:11
【问题描述】:

以便 Visual Studio 设计器在显示 XAML 文件的 GUI 时调用依赖注入容器,而不是使用默认构造函数

【问题讨论】:

  • 为什么投-1票?

标签: c# wpf visual-studio xaml dependency-injection


【解决方案1】:

不,我不这么认为,但您应该能够使用 GetIsInDesignMode 属性来检测您是否处于设计模式并相应地设置您的依赖项:

public YourView()
{
    ...
    if (DesignerProperties.GetIsInDesignMode(this))
    {
        //set up design time dependencies here...
    }
}

【讨论】:

  • 这是我目前所做的,但我的仅用于设计模式的代码正在堆积,所以我正在寻找减少这种情况的方法。设计师可见的 DI 容器似乎是一个可能的答案。基本上我的大部分视图都调用了 GetIsInDesignMode。
猜你喜欢
  • 2018-09-13
  • 1970-01-01
  • 2016-12-06
  • 2013-04-30
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多