MVVM, use new Windows or Views?

2019-09-05 01:43发布

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:

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.

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.

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?

Thanks!

2条回答
Rolldiameter
2楼-- · 2019-09-05 02:12

you can simply open a new dialog/window to fill your details. check out my apporach here

查看更多
老娘就宠你
3楼-- · 2019-09-05 02:27

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.

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.

For more information on this, check out the Wpf Application Framework.

查看更多
登录 后发表回答