C# WPF中MVVM的项目结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18825888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:20:31  来源:igfitidea点击:

Project structure for MVVM in WPF

c#wpfxamlmvvm

提问by user2381422

What is the project structure you end up with when using MVVM in WPF?

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

From the tutorials I saw now, they usually have folders: Model, ViewModel and View.

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

In Model you put classes like Person for example that capture data and logic.

在 Model 中,您可以放置​​像 Person 这样的类来捕获数据和逻辑。

In ViewModel you instantiate classes defined in Model. The View contains .xaml files.

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

Edit: I edit my original post to send an example project structure. I have question related to this. How do I organize these: App.config App.xaml MainWindow.xaml

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

Should I leave them outside like they are now or should I put them in some folder?

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

enter image description here

在此处输入图片说明

采纳答案by Sheridan

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 Personclass 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:

您已经描述了通常的或常见的文件夹布局。根据经验,我更喜欢为模型数据类型添加一个单独的文件夹(或大型应用程序中的项目),例如Person您提到的典型类。我这样做的原因是因为这通常会成为最大的项目之一。我还将其拆分为以下子文件夹:

DataTypes
    Collections
    Enums
    Interfaces

I also have separate folders (or projects in large applications) for the application Converterclasses, 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:

我还有单独的文件夹(或大型应用程序中的项目)用于应用程序Converter类、扩展方法类、实用程序(或服务)类。最后,我有与应用程序文件夹结构非常匹配的测试项目。总的来说,这大致是我的文件夹的样子:

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 Collectionsfolder/project will be in the ApplicationName.DataTypes.Collectionsnamespace. Classes in the Data Providersfolder/project will have the ApplicationName.Models.DataProvidersnamespace.

项目,就像文件夹一样,只是提供分离级别。他们还帮助我规划我的应用程序命名空间。例如,Collections文件夹/项目中的代码类将在ApplicationName.DataTypes.Collections命名空间中。在类Data Providers文件夹/项目将有ApplicationName.Models.DataProviders命名空间。

Furthermore, in large applications, my project names come from their location in this hierarchy... for example, my DataTypesproject is actually called ApplicationName.DataTypesand my Modelsproject is called ApplicationName.Models. The Collectionsand DataProvidersparts are folders, along with all of the items past the second level, eg. Enums, Images, Commands, etc.

此外,在大型应用程序中,我的项目名称来自它们在这个层次结构中的位置……例如,我的DataTypes项目实际上被称为ApplicationName.DataTypes,我的Models项目被称为ApplicationName.Models。在CollectionsDataProviders零件文件夹,所有的项目过去的第二个层次,例如一起。EnumsImagesCommands,等。

回答by Jaster

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
  • 主应用程序 (.exe) - 全局样式等
  • Common Lib WPF - WPF 的基类和助手
  • Common Lib General - 模型的基类和助手
  • 基础设施 - 依赖注入、日志记录等
  • 虚拟机接口
  • M 接口
  • 几个包含视图和相应视图模型的库 - 在这里也可以拆分
  • 几个包含模型的库

All dependencies are based on Interfaces only resolved via DI.

所有依赖项都基于仅通过 DI 解析的接口。

回答by Benoit Blanchon

Most people use the "standard" structure you mentioned:

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

  • Model/
    • CarModel.cs
    • DriverModel.cs
  • ViewModel/
    • CarViewModel.cs
    • DriverViewModel.cs
  • View/
    • CarView.xaml
    • DriverView.xaml
  • 模型/
    • 汽车模型.cs
    • 驱动模型.cs
  • 视图模型/
    • CarViewModel.cs
    • 驱动视图模型.cs
  • 看法/
    • 汽车视图.xaml
    • 驱动视图.xaml

I think the reason why it's popular is because some people will argue that you should be able to put Models, ViewModels and Views in different assemblies.

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

Also with this structure, you can easily add folders for other WPF stuffs: Converters/, Resources/, etc.

还利用这种结构,您可以轻松地将文件夹添加其他东西WPF: ,Converters/Resources/等。

Within my team, we use this structure but we pluralize the names (so Models/ViewModels/Views).

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

However, most of the time, model classes are defined in other assemblies/namespace; in that case, we don't even have a Models/folder.

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

For large projects, we add subfolders into the Models/, ViewModels/and Views/

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

For the sake of completeness, it's worth mentioning that you may find a few people using a "feature driven" structure:

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

  • Car/
    • CarModel.cs
    • CarViewModel.cs
    • CarView.xaml
  • Driver/
    • DriverModel.cs
    • DriverViewModel.cs
    • DriverView.xaml
  • 车/
    • 汽车模型.cs
    • CarViewModel.cs
    • 汽车视图.xaml
  • 司机/
    • 驱动模型.cs
    • 驱动视图模型.cs
    • 驱动视图.xaml

But it's very uncommon.

但这是非常罕见的。

回答by Rodolfo Gaspar

Friends, the solution I found for a problem similar to this was to create a separate project, the type WPF, I called Startup, only with App.xaml (and App.xaml.cs).

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

In it I refer to the project of the View and ViewModel. So the view has no dependence and ViewModel "sees" only the View and Business.

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

In App.xaml.cs declare and instantiate my MainWindow then load some basic properties of my app and navigate to page Login (I'm working with a Window and several pages browsing within them).

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

enter image description here

在此处输入图片说明