67

在 WPF 中使用 MVVM 时,您最终得到的项目结构是什么?

从我现在看到的教程来看,它们通常有文件夹:Model、ViewModel 和 View。

在 Model 中,您可以放置​​诸如 Person 之类的类来捕获数据和逻辑。

在 ViewModel 中,您可以实例化 Model 中定义的类。视图包含 .xaml 文件。

编辑:我编辑我的原始帖子以发送示例项目结构。我有与此相关的问题。如何组织这些: App.config App.xaml MainWindow.xaml

我应该像现在一样将它们留在外面还是应该将它们放在某个文件夹中?

在此处输入图像描述

4

4 回答 4

73

You have described the usual or common folder layout. From experience, I prefer to add a separate folder (or project in large applications) for the model data type, such as the typical Person class that you mentioned. The reason that I do this is because this often becomes one of the biggest projects. I also split it into the following sub folders:

DataTypes
    Collections
    Enums
    Interfaces

I also have separate folders (or projects in large applications) for the application Converter classes, extension method classes, utility (or service) classes. Finally, I have test projects that pretty much match the application folder structure. In total, this is roughly what my folders look like:

Solution

    Third Party Libraries <<< (Solution Folder)

    StartUp Project
        Images
        Resources

    Converters

    DataTypes
        Collections
        Enums
        Interfaces <<< (For Data Type classes)

    Extensions

    Models
        Data Controllers
        Data Providers
        Interfaces <<< (For swapping Model classes out in test projects)

    Utilities (Or Services)
        Interfaces <<< (For swapping Utilities classes out in test projects)

    View Models
        Commands

    Views
        Attached Properties
        Controls

UPDATE >>>

Projects, like folders, just provide levels of separation. They also help me to map out my application namespaces. For example, code classes in the Collections folder/project will be in the ApplicationName.DataTypes.Collections namespace. Classes in the Data Providers folder/project will have the ApplicationName.Models.DataProviders namespace.

Furthermore, in large applications, my project names come from their location in this hierarchy... for example, my DataTypes project is actually called ApplicationName.DataTypes and my Models project is called ApplicationName.Models. The Collections and DataProviders parts are folders, along with all of the items past the second level, eg. Enums, Images, Commands, etc.

于 2013-09-16T10:50:32.897 回答
32

大多数人使用您提到的“标准”结构:

  • 模型/
    • 汽车模型.cs
    • DriverModel.cs
  • 视图模型/
    • CarViewModel.cs
    • DriverViewModel.cs
  • 看法/
    • CarView.xaml
    • DriverView.xaml

我认为它受欢迎的原因是因为有些人会争辩说你应该能够将模型、视图模型和视图放在不同的程序集中。

同样使用这种结构,您可以轻松地为其他 WPF 内容添加文件夹:Converters/Resources/等。

在我的团队中,我们使用这种结构,但我们将名称复数(例如 Models/ViewModels/Views)。

然而,大多数时候,模型类是在其他程序集/命名空间中定义的;在这种情况下,我们甚至没有Models/文件夹。

对于大型项目,我们将子文件夹添加到Models/,ViewModels/Views/

为了完整起见,值得一提的是,您可能会发现一些人使用“功能驱动”结构:

  • 车/
    • 汽车模型.cs
    • CarViewModel.cs
    • CarView.xaml
  • 司机/
    • DriverModel.cs
    • DriverViewModel.cs
    • DriverView.xaml

但这非常罕见。

于 2013-09-16T11:02:24.290 回答
4

朋友们,我找到的类似问题的解决方案是创建一个单独的项目,WPF 类型,我称为 Startup,仅使用 App.xaml(和 App.xaml.cs)。

在其中我指的是 View 和 ViewModel 的项目。因此视图没有依赖关系,ViewModel 只“看到”视图和业务。

在 App.xaml.cs 中声明并实例化我的 MainWindow 然后加载我的应用程序的一些基本属性并导航到页面登录(我正在使用一个窗口和几个在其中浏览的页面)。

在此处输入图像描述

于 2016-06-21T16:46:52.230 回答
3

What I usualy have goes like this:

  • Main Application (.exe) - Global Styles, etc
  • Common Lib WPF - Base Classes and Helpers for WPF
  • Common Lib General - Base Classes and Helpers for Models
  • Infrastructure - Dependency Injection, Logging, etc
  • Interfaces for VM
  • Interfaces for M
  • Several Libraries containing Views and corresponding ViewModels - Splitting here is possible as well
  • Several Libraries containing Models

All dependencies are based on Interfaces only resolved via DI.

于 2013-09-16T10:51:55.330 回答