MVVM,使用新的Windows还是View? [英] MVVM, use new Windows or Views?
问题描述
在完成一些常规项目之后,我最近的项目使用Caliburn.Micro实现了MVVM模式-但我仍然遇到一些体系结构问题。主要是:
After finishing some by-hand projects, my recent project implements the MVVM pattern using Caliburn.Micro - but I still struggle with some architectural problems. Mainly:
我的ViewModel包含一个 BindableCollection
ProfilesCollection
个人资料
。我的视图有一个添加新配置文件的按钮。但是由于配置文件很复杂,我希望单击按钮以打开一个新窗口,在其中可以填写详细信息,然后返回到主窗口。
My ViewModel contains a BindableCollection
ProfilesCollection
of Profiles
. My view has a button to add new profiles. But since the profiles are complex I want the button click to open a new window where I can fill in the details, and then return back to the main window.
在我以前的手工方法中,我将 ProfilesCollection
设为静态,以便可以在整个应用程序中访问它,并且在单击事件中,我刚刚打开了一个新窗口,然后将新配置文件存储在静态 Collection
中。
In my previous by hand approach I made the ProfilesCollection
static so that it was accessible throughout the whole application, and as click event I just opened a new window, where I then stored the new profile in the static Collection
.
正确的MVVM方法是什么?创建一个新的视图/视图模型而不是一个新的窗口?如何管理它们,如何将创建的 Profile
传递回MainViewModel?
What would be the right MVVM approach? Create a new view/ viewmodel instead of a new window? How do I manage them, and how do I pass the Profile
I create back to the MainViewModel?
谢谢!
推荐答案
我会先通过 ProfilesCollection
放入 ProfilesViewModel
以及 ProfilesView
来选择所需的配置文件
I would start by passing the ProfilesCollection
into a ProfilesViewModel
along with a ProfilesView
for selecting the profile you want to edit and for adding/removing profiles.
第二,我将有一个单独的 ProfileViewModel
和 ProfileView
。 ProfileViewModel
将具有 ShowDialog
方法,该方法将调用 ShowDialog
在 ProfileView
子窗口中。这将允许 ProfilesViewModel
在 ProfileViewModel
上设置配置文件,然后调用 ShowDialog
,非常适合MVVM模式。
Second, I would have a separate ProfileViewModel
and ProfileView
. The ProfileViewModel
would have a ShowDialog
method that would call ShowDialog
on the ProfileView
child window. This will allow the ProfilesViewModel
to set the profile on the ProfileViewModel
and then call ShowDialog
, which will fit the MVVM pattern nicely.
有关此的更多信息,请查看 Wpf应用程序框架。
For more information on this, check out the Wpf Application Framework.
这篇关于MVVM,使用新的Windows还是View?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!