简体   繁体   English

IoC - 使用注入的对象参数解析 WPF 窗口

[英]IoC - resolve WPF windows with injected object parameters

My first attempt on using an IoC container.我第一次尝试使用 IoC 容器。 A program has a number of different type of windows that are typically opened through a menu in MainWindow .一个程序有许多不同类型的窗口,通常通过MainWindow的菜单打开。 However, in principle it could also be further down the chain.然而,原则上它也可以在链条的更下游。 These windows usually need injection of one or more singleton-classes, lets name them here IManager1/2/3 .这些窗口通常需要注入一个或多个单例类,我们将它们命名为IManager1/2/3 So, do I need to bubble these IManagers up through MainWindow , or can I resolve each window at the root (here in App class )?那么,我是否需要通过MainWindow将这些IManagers冒泡,或者我可以在根(在App class )解析每个窗口吗?

In either case, how would this be done?在这两种情况下,这将如何完成?

I don't need to use DryIoc, but that's just what I chose to test.我不需要使用 DryIoc,但这正是我选择测试的。

   public partial class App : Application
{
    DryIoc.Container container = new DryIoc.Container();

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        RegisterIoc();

        var mainwindow = new MainWindow(); 
        mainwindow.Show();
    }

    public void RegisterIoc()
    {
        container.Register<IManager1, Manager1>(Reuse.Singleton);
        container.Register<IManager2, Manager2>(Reuse.Singleton);
        container.Register<IManager3, Manager3>(Reuse.Singleton);
    }
}

public MainWindow()
{
    public MainWindow()
    {
    }

    void OpenNewWindow2()
    {
        var w = new Window2(?, ?, ?);
        w.Show();
    }
}

public class Window2
{
    IManager1 man1;
    IManager2 man2;
    IManager3 man3;

    public Window2(IManager1 man1, IManager2 man2, IManager3 man3)
    {
        this.man1 = man1;
        this.man2 = man3;
        this.man3 = man3;
    }
}

Consider following explicit dependency principle and making Window2 a dependency of MainWindow考虑遵循显式依赖原则并使Window2成为MainWindow的依赖

For example例如

public class MainWindow {
    private readonly Func<Window2> window2;

    public MainWindow(Func<Window2> window2) {
        this.window2 = window2;
    }

    public void OpenNewWindow2() {
        var window = window2(); // invoke factory delegate
        window.Show();
    }
}

The above shows the use of a Func<T> as a factory delegate which will act as a wrapper for deferred resolution of the required dependency.上面显示了使用Func<T>作为工厂委托,它将充当延迟解析所需依赖项的包装器。 Every time the factory delegate is invoked it will resolve a new instance of Window2 , inverting the control of MainWindow having to initialize it manually;每次调用工厂委托时,它都会解析一个新的Window2实例,反转MainWindow的控制,必须手动初始化它;

The App.Application_Startup is used as the composition root where you would register every thing with the container and resolve the MainWindow , your root object App.Application_Startup用作组合根,您可以在其中向容器注册所有内容并解析MainWindow ,即您的根对象

public partial class App : Application {
    DryIoc.Container container;

    private void Application_Startup(object sender, StartupEventArgs e) {
        container = RegisterIoc();    
        var mainwindow = container.Resolve<MainWindow>(); 
        mainwindow.Show();
    }

    private DryIoc.Container RegisterIoc() {
        var container = new DryIoc.Container();
        container.Register<MainWindow>();
        container.Register<Window2>();
        container.Register<IManager1, Manager1>(Reuse.Singleton);
        container.Register<IManager2, Manager2>(Reuse.Singleton);
        container.Register<IManager3, Manager3>(Reuse.Singleton);
        return container;
    }
}

Note that for the windows to be resolved you also need to make sure they are also registered with the container.请注意,对于要解析的窗口,您还需要确保它们也已在容器中注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM