【发布时间】:2019-11-20 04:34:09
【问题描述】:
在现有的 WPF 应用程序中,我想实现依赖注入。 所以在我的应用程序启动时,我设置了 di 容器并让窗口像这样构建:
var builder = new ContainerBuilder();
builder.RegisterType<SplashScreen>().AsSelf();
builder.RegisterType<ILogger>().As(Logger);
Container = builder.Build();
using (var scope = Container.BeginLifetimeScope())
{
var window = scope.Resolve<SplashScreen>();
window.Show();
window.Initialiseren();
}
在我的窗口中,我有一个按钮可以调用具有多个依赖项的另一个窗口?
public partial class AnotherWindow
{
public AnotherWindow(ILogger)
{
...
}
}
public partial class Window
{
public void Button_Click()
{
AnotherWindow w = new AnotherWindow(new Logger());
w.Show();
}
}
我如何使用我的容器来解析另一个窗口,而无需将我的容器到处传递? 我的目标是使用 autofac 初始化 ILogger。
提前谢谢你!
【问题讨论】:
标签: c# wpf dependencies code-injection