如何使用WPF尊重的良好做法,以打开一个新窗口? [英] How to open a new window using WPF respecting good practices?

查看:121
本文介绍了如何使用WPF尊重的良好做法,以打开一个新窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过与WPF(GUI和一般的)一个很好的做法说要打开的几个窗口越好。但有时候,你根本就没有选择

I've read a good practice with WPF (and GUI in general) saying to open as few windows as possible. But sometimes, you simply don't have the choice.

于是,我想到了一个快速的一个优雅的解决打开一个新窗口,我想这样:

So I thought about a fast an elegant solution to open a new window and I thought to this:

public static class WinManager
{
    private static Dictionary<Type, Func<Window>> collection 
        = new Dictionary<Type, Func<Window>>();

    /* Bind the type of the ViewModel with a lambda that build an 
     * instance of a window*/
    public static void Bind(Func<Window> ctor, Type type) { ... }

    /* Search in the dictionary the specified type and show the window 
     * returned by the lambda*/
    public static void Show(Type type){ ... }

    /* Search in the dictionary the specified type and show the dialogue
     * returned by the lambda*/
    public static void ShowDialog(Type type) { ... }
}

键入是用于返回窗口的一个新的实例的视图模型的绑定到视图(这是窗口)和拉姆达男星的类型。

typeis the type of the ViewModel binded to the View (that's the window) and the lambda ctor is used to return a fresh instance of a window.

这是个好主意来管理这样的窗口还是我完全错了?

Is it a good idea to manage windows like this or am I totally wrong?

推荐答案

我认为这是一个体面的想法。

I think it is a decent idea.

它有希望显示另一个窗口视图模型没有使用任何WPF特定的代码,甚至不知道的好处风景。它只需要知道它要展示的窗口视图模型。这是非常相似的 Caliburn.Micros IWindowManager

It has the advantage that the ViewModel that wants to show another window doesn't have to use any WPF specific code or even know the view. It only needs to know the ViewModel it wants to show the window for. This is very similar to Caliburn.Micros IWindowManager.

我不喜欢这个解决方案就是类的静态特性。这使得单元测试硬(ER)。如果您正在使用依赖注入,你可以创建一个接口,该接口类似于静态类的实现。然后,你可以在你的作文根创建类的实例,绑定视图模型类型拉姆达查看工厂,并与DI容器注册该实例。 。想要现在显示另一个窗口每个视图模型有一个接口,这使得它在单元测试中轻松mockable的依赖关系。

The thing I don't like about this solution is the static nature of the class. This makes unit testing hard(er). If you are using dependency injection, you could create an interface and an implementation of that interface that is similar to your static class. You could then create an instance of that class in your composition root, bind the ViewModel types to the lambda View factories and register that instance with your DI container. Every ViewModel that wants to show another window now has a dependency on that interface which makes it easily mockable in unit tests.

事情是这样的:

interface IWindowManager
{
    void Show(Type type);
    void Show<T>();
    void ShowDialog(Type type);
    void ShowDialog<T>();
}

class WindowManager : IWindowManager
{
    // Implementation of the four methods from the interface plus:

    private Dictionary<Type, Func<Window>> collection 
        = new Dictionary<Type, Func<Window>>();

    public void Bind(Func<Window> ctor, Type type) { ... }
}

具有绑定方法只能在具体的实施窗口管理器的好处就是消费者 IWindowManager 无法变更登记。

Having the Bind method only on the concrete implementation WindowManager has the benefit that consumers of IWindowManager can't change the registration.

这篇关于如何使用WPF尊重的良好做法,以打开一个新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆