wpf 在 MVVM 中创建 ViewModel 的最佳位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12500930/
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
Best place to create a ViewModel in MVVM
提问by Serhii Kyslyi
I have a question: Where is the best place to create the ViewModel in MVVM and how?
我有一个问题:在 MVVM 中创建 ViewModel 的最佳位置在哪里以及如何创建?
1) Create once in App.xaml.cs as static field and then use it through App?
1) 在 App.xaml.cs 中创建一次作为静态字段,然后通过 App 使用它?
2) Alway create new ViewModel in Page.cs, when I navigate to this page?
2) 当我导航到这个页面时,总是在 Page.cs 中创建新的 ViewModel?
3) other options
3)其他选项
回答by Rachel
In MVVM, the ViewModel isthe application. This means that I usually have an single startup ViewModel that is the entry point to my application, and I typically create an instance of this in the App.xaml.cs OnStartupcode
在 MVVM 中,ViewModel就是应用程序。这意味着我通常有一个单一的启动 ViewModel 作为我的应用程序的入口点,我通常在 App.xaml.csOnStartup代码中创建一个实例
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var app = new ShellView();
var context = new ShellViewModel();
app.DataContext = context;
app.Show();
}
Every once in a while I have an application that will create the ViewModel in the startup window's constructor, but this is not really preferred because it means if I have any startup logic, I have to put that in the code-behind the View as well, and I don't like mixing application logic in my View layer.
每隔一段时间我就有一个应用程序会在启动窗口的构造函数中创建 ViewModel,但这并不是真正的首选,因为这意味着如果我有任何启动逻辑,我也必须将它放在视图后面的代码中,而且我不喜欢在我的 View 层混合应用程序逻辑。
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ShellViewModel();
}
}
Regardless of how you do it, keep in mind that when using MVVM, your ViewModels are your application, not your Views, so typically your ViewModels are connected in some way to the startup ViewModel. Views are just a user-friendly way for users to interact with your application (the ViewModels).
无论您如何操作,请记住,在使用 MVVM 时,您的 ViewModel 是您的应用程序,而不是您的 View,因此通常您的 ViewModel 以某种方式连接到启动 ViewModel。视图只是用户与您的应用程序(ViewModels)交互的一种用户友好方式。
回答by Damascus
There are different ways to do it, depends on what you think.
有不同的方法来做到这一点,取决于你的想法。
I personally always have a class designed to create all the objects I need, called in the App.xaml.cs.
The class basically does those time-consuming startup procedures while a splashscreen is displayed. I create both Views and ViewModels here AND link them
我个人总是有一个类旨在创建我需要的所有对象,在App.xaml.cs. 该类基本上在显示启动画面时执行那些耗时的启动过程。我在这里创建了视图和视图模型并链接它们
This allows me to have one and only one point in which all those links View-to-ViewModel are created, I can come back to it easily, even if I add/remove something.
这允许我拥有一个且只有一个点来创建所有这些 View-to-ViewModel 的链接,即使我添加/删除某些内容,我也可以轻松地回到它。
I don't like the approach in which you initialize the viewModel in each view's constructor. Assuming you have 15 views in your project, you'd have 15 different files to browse if you want to check all the ViewModel initializations.
我不喜欢在每个视图的构造函数中初始化 viewModel 的方法。假设您的项目中有 15 个视图,如果您想检查所有 ViewModel 初始化,您将有 15 个不同的文件可供浏览。
This is my humble participation to this =)
这是我对这个的谦虚参与 =)
回答by Big Daddy
You can use dependency injection and create it like this (assuming you're using some DI container):
您可以使用依赖注入并像这样创建它(假设您正在使用一些 DI 容器):
public partial class YourView : UserControl
{
public YourView (IYourViewModel viewModel)
{
InitializeComponent();
this.DataContext = viewModel;
}
}

