简体   繁体   English

WPF在MVVM中打开单独的窗口

[英]WPF open seperate windows in MVVM

I am new in WPF and I am trying to keep the MVVM pattern. 我是WPF的新手,我正在尝试保持MVVM模式。
I have a main form that it will presents some reports (in the form of tiles but it really doesn't matter). 我有一个主要的形式,它将呈现一些报告(以磁贴的形式,但这并不重要)。
So I want if the user double click one of the reports to open the report in a new seperate window. 因此,我希望用户双击报告之一以在新的单独窗口中打开报告。
What i did and I know that this is not the correct MVVM approach is that I have this command at the ReportsmanagerViewModel 我做了什么,我知道这不是正确的MVVM方法,因为我在ReportsmanagerViewModel中有此命令

Private Sub OpenReportExecute()
    Dim win As New ReportView
    win.Show()
End Sub

I don't use PRISM or any other framework. 我不使用PRISM或任何其他框架。 How can I open the window without breaking the MVVM? 如何在不破坏MVVM的情况下打开窗口?

MVVM should avoid referencing presentation assemblies from your view models. MVVM应该避免从您的视图模型中引用表示形式的程序集。 You need to call the new window from your view model, so how do you break the dependency? 您需要从视图模型中调用新窗口,那么如何打破依赖关系呢?

The same way we break every dependency - an interface! 我们打破每个依赖关系的方式- 接口!

I usually call this an interaction service as a window is an interactivity object. 我通常称其为交互服务,因为窗口是交互对象。 There may be varying opinions on this. 对此可能会有不同的意见。 YMMV etc. YMMV等

public interface IDataInteractionService
{
    //Implementations will display the data SOMEHOW
    void DisplayData(Data d);
}

And now the implementation 现在执行

//Displayed data using windows!
public class WindowedDataInteractionService: IDataInteractionService
{
    public void DisplayData(Data d)
    {
        new Window().ShowDialog(); //basic implementation shows a window.
    }
}

The trick here is that your view model does not reference any WPF assemblies directly - this is called indirection . 这里的技巧是,视图模型不直接引用任何WPF程序集-这称为间接 As a bonus, you have an interface so swapping the service implementation is possible with confidence that you wont break the view model code. 另外,您还有一个接口,因此可以放心地交换服务实现,而不会破坏视图模型代码。 This is an example of SOLID principles making life easier for future you. 这是SOLID原则的示例,使您的未来生活更加轻松。

You are not using PRISM or other Dependency Injection Framework , so a challenge for you will be getting the service to your view model. 您没有使用PRISM或其他依赖项注入框架 ,因此面临的挑战是将服务引入您的视图模型。 Common practice is to use a Singleton implementation to make your IDataInteractionService avaliable to the application. 通常的做法是使用Singleton实现使IDataInteractionService于该应用程序。

Here is a example how you should do that, its in C# but you should have an Idea how to do it: Open a new Window in MVVM 这是一个示例,您应该如何使用C#进行操作,但是应该有一个操作方法: 在MVVM中打开一个新窗口

Sorry for answering 2 times :) it was a mistake. 抱歉回答2次:)这是一个错误。 The approach in this example is to Delegate the creation of that new Window in a FactoryClass which is responsible to create Windows Objects, set the DataContext to it and call method Show. 本示例中的方法是委托负责创建Windows对象的FactoryClass中的新Window的创建,将DataContext设置为它,并调用Show方法。 With this you solve 2 Problems: 这样您可以解决2个问题:

  1. Your ViewModel should not open other Windows, and Create other ViewModels 您的ViewModel不应打开其他Windows,并创建其他ViewModel

  2. You resolve Dependencies in your class. 您可以在班级中解决依赖关系。 You place the creation of your Window in a Factory class. 您将创建的Window放置在Factory类中。

This is important for UnitTesting your ViewModel. 这对于单元测试ViewModel非常重要。 With much Dependencies it is harder to Test your ViewModel. 由于存在很多依赖性,因此很难测试您的ViewModel。

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

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