My WPF application follows the MVVM pattern. There are three views:
- MainWindow
- LoginView
- ProjectsView
LoginView
and ProjectsView
are user controls imported by the MainWindow
. Both views have their view model assigned. LoginViewModel
defines a property ProjectList
which is set by calling a webservice. Now LoginViewModel
needs access to the ProjectList
property and others.
I am aware that one solution might be a redesign so that there is only one view and one view model. I would do that as a backup solution but I would favor not to do so.
How should this be done? Should I use some kind of EventAggregator like in Prism? Or are there other ways to do this?
Here are a few ideas:
Also, I believe that the
EventAggregator
is best suited for communicating between modules/assemblies. In your example, it seems like everything is in the same assembly.So if i understood clearly,
ProjectList
property should be accessed from both 'LoginViewModel' and 'ProjectsViewModel'. I'd try to implement it in the 'MainViewModel' so child viewmodels can access it in a natural way.An IEventAggregator is like a box in which you can add events, or find and subscribe to one, so i would say it's not what you need. Anyway, you could register your custom interface (box type) in the
UnitySingleton.Container
, which would exposeProjectList
for it to be accessible everywhere. This approach makes a lot of sense when modules, which are separate assemblies, need to communicate whith each other. If this is overkill or not in your case is something you should decide, i'd personally go with the 'put it in the mainviewmodel' option.-- Sample -- (not tested)
It's pretty simple as you see,
LoginVM
andProjectsVM
will hold a reference to theMainViewModel
that created them, therefore giving them access toProjectList
.