i was at the bar and i had couple beers, and this girl was arguing that getting mvvm to work with a real world applications is pain, she said that in order to solve problem in mvvm you add more code then you get another problem and the you add more code and it never ends, and i agree, when i read about mvvm it sounded nice, and i don't want to use prism nor MVVM light, i just need a way to switch between my views. and my application looks like this so please inspire me before i get alcohol poisoning
Asked
Active
Viewed 706 times
3
-
Does real world application equal a very cluttered UI? – flup Oct 04 '13 at 21:27
-
@flup i made such UI because i thought its the best way to learn it – user1590636 Oct 04 '13 at 21:29
-
I have no idea what you're talking about. MVVM is cleaner and requires less code than anything else I've ever seen. Just put some `
`s there and bind them to some relevant ViewModels. What is your question? – Federico Berasategui Oct 04 '13 at 21:30 -
Yes. If you want loosely-coupled communication you need an EventAggregator (not specifically in MVVM but in any other loosely-coupled architecture you can think of). And you could implement this type of UI just by having a single `MainViewModel` with some properties to determine what to show in the smaller Views. There are thousands of ways to implement this, you may choose whatever you like the most. MVVM is not a set of hardcore rules, it's just a set of guidelines, the most important of which is "Don't put business logic in Code-Behind" – Federico Berasategui Oct 04 '13 at 21:35
-
It's just a class that has an event and subscribers handle that event and react upon it. And it also has a void method that you can call to have it raise the event – Federico Berasategui Oct 04 '13 at 21:41
-
You were arguing with a girl in a bar... about MVVM? ...where is that bar, anyway? – Yandros Oct 04 '13 at 22:44
1 Answers
7
If you want loosely-coupled communication you need an EventAggregator:
//Simplest EventAggregator
public static class DumbAggregator
{
public static void BroadCast(string message)
{
if (OnMessageTransmitted != null)
OnMessageTransmitted(message);
}
public static Action<string> OnMessageTransmitted;
}
Usage:
public class MySender
{
public void SendMessage()
{
DumbAggregator.BroadCast("Hello There!");
}
}
public class MySubscriber
{
public MySubscriber()
{
DumbAggregator.OnMessageTransmitted += OnMessageReceived;
}
private void OnMessageReceived(string message)
{
MessageBox.Show("I Received a Message! - " + message);
}
}
And if you don't like Child ViewModels, you can put everything inside a single ViewModel, and have some DataTriggers
or something to dynamically change views, however a Parent - Children ViewModel approach is much cleaner IMO.

Federico Berasategui
- 43,562
- 11
- 100
- 154
-
its simpler than i thought, for example can i run a command that is on another view model using this? – user1590636 Oct 04 '13 at 22:08
-
1@user1590636 yes. You can pretty much do anything you need without creating coupling between your VMs / Layers. Notice however that EventAggregators included in frameworks such as Prism are much more complex and include a whole lot of functionality. – Federico Berasategui Oct 04 '13 at 22:11
-
1i tested it and its lovely i had a log view model with a progress bar and its really cool to send messages from all view models to the log view model and increase the progress bar without have them to know any thing about each other especially when you are really drunk – user1590636 Oct 04 '13 at 22:37
-
Thank you very much for the example. I was sitting on this for hours, trying to figure out how to make it work! – Mattjo Jun 10 '21 at 12:51