@@BrianLagunas Very true, I learned a lot from him. Because of Brian, I became confident to work with WPF. Now I really embrace all cool things you can do with both of these technologies. Flicker Free Rendering, Custom Control Development is childs play and fantastic clean scalable architecture which Prism provides.
Thanks a lot for your simple and wonderful tutorial, was hearing your names for a very long time and was reading your blogs, happy to see you contributing here. And like others expecting more videos about prism as there would be no better person than you as of now for this :), we know the history of prism :). Thanks
Thank you for the example, was really useful. Additionally, I created a simple object for the data of the ListBox instead of using string and use an Item Template with some styles and binding a string property of that object with the message. Greetings from Argentina!
Cool! I like how simple this is. Bravo on designing this! I think, though, that when you're working with a data access project (which you probably always are), then you should put your events in that project because you're going to want to pass around your classes in the events. Otherwise, the example is very clear and good!
Oh, man, that was really simple and tight. This should be the official sample rather than the convoluted example in the official documentation. Instant sub.
Great video for a solution I've been searching for but I cant seem to get past "the viewmodel locator does not exist in the prism namespace". Any idea what I'm missing?
Excellent tutorial and very easy to implement. Thanks. But what if subscriber or publisher are not in the ViewModel class, in that case we can not just ask for IEventAggregator in constructor. How to get IEventAggregator inside plain class?
If that class is a service, then it will automatically be injected by the container when the class is resolved. If you're talking about a business model class, that's code smell however still can be done with the ServiceLocator pattern.
Hello there! Started learning prism. Can i ask u - why do prism 6 for wpf documentation is less than documentation for 5 version? 10 topics for 6 version vs 18 topics for 5 version. I where can i get source code of example app from documentation? Thank u for ur work!
Am I missing something on the MainWindow? I keep getting this exception: System.NullReferenceException: 'Object reference not set to an instance of an object. I've watched the video several times but cannot seem to find anywhere where you show what you did to the InitializeComponant(); method, if anything. If anyone can enlighten me I would appreciate it.
Hard to say without seeing your code, but if you are getting a build error with the InitialComponent, make sure the x:class in the xaml matches the namespace of the code behind file. This can get messed up if you move the MainWindow file to another folder and changed the namespace of the c# file
Hi Brian, Thank you somuch for wondeful video. However, I am searching for a similar solution but not as your application depending on multiple sub applications rather in the same application between multiple view models. I tried the similar way but cannot overcome the null reference exception. Is it possible to get the source code of this tutorial to check how you have managed master or main view model. Thanks and Regards Anil
yes, there is a NullReference exception at ea.GetEvent.Subscribe(Method) .FYI, my MVVM structure is a bit different from you. I have defined a IEventAggregator in main window model and passed it into the subscriber and publisher view models. One of other warnings for me shown as IEventAggregator defined in the mainwindow model will be always null as it has been never set or initialized. It would be nice if you could tell me how to set it or initialize as other variables or classes. Apart from that, I am beginner started learning events recently and came across Prism which I found reduces programming effort and also makes program easy.
@@badboy13592 You need to find out why your EventAggregator (EA) instance is null. Are you creating an instance of it anywhere. Doesn't sound like you are using Dependency Injection, so you'll need to create the instance yourself and make sure you are using the same instance everywhere. static EA ea = new EA() would do it.
Thank you for this video! Explained simply and succinctly. I tried to put this in use in a WPF .NET Core 3.1 application. I have two independent modules and everything set-up like in your example (I obtained reference to aggregator instance via DI in constructor like in your example, and it's the same instance of aggregator on both publisher's and subscriber's side), yet the subscriber does not receive published message. The event is defined in a third module referenced by the first two modules to get hold of the event type). I am not using shared libraries either (both are .NET Core 3.1. class libraries). Is that .NET Core that is a problem here or is there anything else I might have done wrong?
Hi Brian, How do I make a decision on the selection between Eventaggregator and Composite Commands? If there a rule of thumb? I have different views injected into my shell under different RegionNames. If one button is clicked in any of the views, I need to switch to a different shell by using some IShellServices from my current shell. In this situation, I can achieve the goal by using both methods. May I know the best practice for this scenario? Thanks.
There are two primary differences between these two. CanExecute for Commands. A Command can say whether or not it is valid for execution by calling Command.RaiseCanExecuteChanged() and having its CanExecute delegate return false. If you consider the case of a "Save All" CompositeCommand compositing several "Save" commands, but one of the commands saying that it can't execute, the Save All button will automatically disable (nice!). EventAggregator is a Messaging pattern and Commands are a Commanding pattern. Although CompositeCommands are not explicitly a UI pattern, it is implicitly so (generally they are hooked up to an input action, like a Button click). EventAggregator is not this way - any part of the application effectively raise an EventAggregator event: background processes, ViewModels, etc. It is a brokered avenue for messaging across your application with support for things like filtering, background thread execution, etc. Hope this helps explain the differences. It's more difficult to say when to use each, but generally I use the rule of thumb that if it's user interaction that raises the event use a command, for anything else use EventAggregator.
What happens when i publish from a background task and one of the subscribers is updating UI (WPF) but is not specifying to use the UIthread. It seems we don't get any errors about this.
Hello Brian. I have 2 questions, can u help me? 1) Where is "Botstrapper" method that register "EventAggregator" for "IEventAggregator". 2) "MessageSentEvent" is isolated class that can be used by both moduleA and moduleB. But what if moduleA has own Events. How moduleB or others can subscribe on them without reffering to moduleA. For example, moduleA has event "BeforeSendText". And i wrote moduleC that needs to get message text from moduleA before it was sent (to lowercase message). So as u can see moduceC needs to subscribe to "BeforeSendText" event of moduleA and for doing that, moduleC must have ref to moduleA. This is not loosely coupled. how can I achieve this and stay loosely coupled?
Thank you Brain. This is what i have looking for months now. You mention that it can't be used for shared library. Will it work for same solution with many usercontrols or viewmodels?
Yes, of course. It just cannot be in a Shared Library because each project referencing the shared library will compile it into it's own assembly creating multiple definitions of the same class. So just make sure you define it in any other project type.
The view you navigate into a region will contain all the functionality of your camera. Nothing changes. Just add your camera code and then navigate to that view.
@@BrianLagunas How to call a button out in viewModel? for example : Button button = .... ( taken from View ) ; button.background = Brushes.Red; => i using prism MVVM
@@BrianLagunas Hmm, I think I might just stick to events through a shared library. My project doesn't really allow for the viewmodels in my shell project to inherit from a Prism class.
I am actually a Pluralsight author :). The EventAggregator is a much better implementation of the MessagingCenter. From the API usage, to being able to filter events, and controlling which thread the events are invoked on. Simply put, the IEventAggregator is much better than the MessagingCenter
My dude, it's 94 seconds into this video before you even start talking about the subject matter. Please stop this madness. This video does not need to be padded like that.
Yeah I learned my lesson when I made these videos, and if you check out my latest videos, you'll see that I don't do this anymore. I was basically copying my Pluralsight course introduction styles. I learn with each video I do. Thanks for the feedback though.
Perfect video!! It makes me feel I don't need to search another video about EventAgregator in youtube anymore!
Wow, thank you so much
WPF is awesome, Brian is awesome, Prism is awesome. Prism makes everything so easy and clean.
Thank you very much for the kind words. You're awesome!
Brian is an awesome teacher... he makes you understand everything
Thank you for the kind words
@@BrianLagunas Very true, I learned a lot from him. Because of Brian, I became confident to work with WPF. Now I really embrace all cool things you can do with both of these technologies. Flicker Free Rendering, Custom Control Development is childs play and fantastic clean scalable architecture which Prism provides.
Thanks a lot for your simple and wonderful tutorial, was hearing your names for a very long time and was reading your blogs, happy to see you contributing here. And like others expecting more videos about prism as there would be no better person than you as of now for this :), we know the history of prism :). Thanks
Thank you so much for the kind words. I will try not to disappoint. Thanks for watching.
Awesome cool feature, solved a very complicated issue superfast after I saw this. Thank you so much Brian.
Thank you for watching. Be sure to share with your community
Thank you for the example, was really useful. Additionally, I created a simple object for the data of the ListBox instead of using string and use an Item Template with some styles and binding a string property of that object with the message. Greetings from Argentina!
Thanks for watching
Cool! I like how simple this is. Bravo on designing this! I think, though, that when you're working with a data access project (which you probably always are), then you should put your events in that project because you're going to want to pass around your classes in the events. Otherwise, the example is very clear and good!
This is great, simple explanation💯
Thanks for watching
Oh, man, that was really simple and tight. This should be the official sample rather than the convoluted example in the official documentation. Instant sub.
Thanks for watching. It is part of the official documentation. Take a look here prismlibrary.github.io/docs/event-aggregator.html
Great video for a solution I've been searching for but I cant seem to get past "the viewmodel locator does not exist in the prism namespace". Any idea what I'm missing?
I was using the wrong package of Prism. I was using PrismCore and not Prism for wpf. Hope this is useful for someone
Thanks... that's really easy to understand. I want to see more videos about Prism.
Be sure to subscribe to my channel so you are notified when new videos are published
Excellent tutorial and very easy to implement. Thanks. But what if subscriber or publisher are not in the ViewModel class, in that case we can not just ask for IEventAggregator in constructor. How to get IEventAggregator inside plain class?
If that class is a service, then it will automatically be injected by the container when the class is resolved. If you're talking about a business model class, that's code smell however still can be done with the ServiceLocator pattern.
@@BrianLagunas Wow, that was an incredibly quick response. I did it in the service class. Thanks, you are the best.
Hello there! Started learning prism. Can i ask u - why do prism 6 for wpf documentation is less than documentation for 5 version? 10 topics for 6 version vs 18 topics for 5 version. I where can i get source code of example app from documentation? Thank u for ur work!
You can find docs here: prismlibrary.github.io/ and you can see samples here: github.com/PrismLibrary/Prism-Samples-Wpf
Am I missing something on the MainWindow? I keep getting this exception: System.NullReferenceException: 'Object reference not set to an instance of an object. I've watched the video several times but cannot seem to find anywhere where you show what you did to the InitializeComponant(); method, if anything. If anyone can enlighten me I would appreciate it.
Hard to say without seeing your code, but if you are getting a build error with the InitialComponent, make sure the x:class in the xaml matches the namespace of the code behind file. This can get messed up if you move the MainWindow file to another folder and changed the namespace of the c# file
Hi Brian,
Thank you somuch for wondeful video.
However, I am searching for a similar solution but not as your application depending on multiple sub applications rather in the same application between multiple view models. I tried the similar way but cannot overcome the null reference exception. Is it possible to get the source code of this tutorial to check how you have managed master or main view model.
Thanks and Regards
Anil
Hard to say what your issue is. What is null? Seems that is the source of your problem.
yes, there is a NullReference exception at ea.GetEvent.Subscribe(Method) .FYI, my MVVM structure is a bit different from you. I have defined a IEventAggregator in main window model and passed it into the subscriber and publisher view models. One of other warnings for me shown as IEventAggregator defined in the mainwindow model will be always null as it has been never set or initialized. It would be nice if you could tell me how to set it or initialize as other variables or classes.
Apart from that, I am beginner started learning events recently and came across Prism which I found reduces programming effort and also makes program easy.
@@badboy13592 You need to find out why your EventAggregator (EA) instance is null. Are you creating an instance of it anywhere. Doesn't sound like you are using Dependency Injection, so you'll need to create the instance yourself and make sure you are using the same instance everywhere. static EA ea = new EA() would do it.
@@BrianLagunas Thanks for reply. It worked but the constructor shall be IEA=EA()
Hi, Brian! What if I can't use Dependency Injection as you did? When I instantiate EventAggregator for subscriber it cames with message Null
I have two windows services and I need to send a message from one to another (.Net Framework 4.7.2)
Thank you for this video! Explained simply and succinctly. I tried to put this in use in a WPF .NET Core 3.1 application. I have two independent modules and everything set-up like in your example (I obtained reference to aggregator instance via DI in constructor like in your example, and it's the same instance of aggregator on both publisher's and subscriber's side), yet the subscriber does not receive published message. The event is defined in a third module referenced by the first two modules to get hold of the event type). I am not using shared libraries either (both are .NET Core 3.1. class libraries). Is that .NET Core that is a problem here or is there anything else I might have done wrong?
Hard to say what your problem is without seeing the code. Put a reproduction app up on github, give me the link, and I'll take a look for you
@@BrianLagunas Ok, please check this out: github.com/Astralius/PrismEventAggregationProblem. I stripped it down to the parts that may matter.
@Brian Lagunas: Have you cloned it yet? :)
@@AzgariusPWI It worked for me 🤷♀️
Issue is now resolved on the linked github, with SO reference (for anyone who might have similar problem).
Hi Brian,
How do I make a decision on the selection between Eventaggregator and Composite Commands? If there a rule of thumb? I have different views injected into my shell under different RegionNames. If one button is clicked in any of the views, I need to switch to a different shell by using some IShellServices from my current shell. In this situation, I can achieve the goal by using both methods. May I know the best practice for this scenario? Thanks.
There are two primary differences between these two.
CanExecute for Commands. A Command can say whether or not it is valid for execution by calling Command.RaiseCanExecuteChanged() and having its CanExecute delegate return false. If you consider the case of a "Save All" CompositeCommand compositing several "Save" commands, but one of the commands saying that it can't execute, the Save All button will automatically disable (nice!).
EventAggregator is a Messaging pattern and Commands are a Commanding pattern. Although CompositeCommands are not explicitly a UI pattern, it is implicitly so (generally they are hooked up to an input action, like a Button click). EventAggregator is not this way - any part of the application effectively raise an EventAggregator event: background processes, ViewModels, etc. It is a brokered avenue for messaging across your application with support for things like filtering, background thread execution, etc.
Hope this helps explain the differences. It's more difficult to say when to use each, but generally I use the rule of thumb that if it's user interaction that raises the event use a command, for anything else use EventAggregator.
putting IEventAggregator in the constructor of the viewmodel is producing an error for me. The view simply doesn't initialize. Did I miss something?
If it's not working you definitely missed something.
What happens when i publish from a background task and one of the subscribers is updating UI (WPF) but is not specifying to use the UIthread. It seems we don't get any errors about this.
So what's the problem exactly?
When I try to do this I need to a parameterless contructor and I don't now why any help would be appreciated!
I have no idea. All I know is that you're doing something wrong :)
Hello Brian. I have 2 questions, can u help me? 1) Where is "Botstrapper" method that register "EventAggregator" for "IEventAggregator". 2) "MessageSentEvent" is isolated class that can be used by both moduleA and moduleB. But what if moduleA has own Events. How moduleB or others can subscribe on them without reffering to moduleA. For example, moduleA has event "BeforeSendText". And i wrote moduleC that needs to get message text from moduleA before it was sent (to lowercase message). So as u can see moduceC needs to subscribe to "BeforeSendText" event of moduleA and for doing that, moduleC must have ref to moduleA. This is not loosely coupled. how can I achieve this and stay loosely coupled?
If events need to be shared, placed them in an assembly that is shared with all projects. Usually this is in some type of Core/Shared project.
Thank you Brian. Please provide a source code link to presented example.
You can find all Prism WPF samples here: github.com/PrismLibrary/Prism-Samples-Wpf
Thank you Brain. This is what i have looking for months now. You mention that it can't be used for shared library. Will it work for same solution with many usercontrols or viewmodels?
Yes, of course. It just cannot be in a Shared Library because each project referencing the shared library will compile it into it's own assembly creating multiple definitions of the same class. So just make sure you define it in any other project type.
Amazing
Yes
Hi! Brain, I have doubt. How to open camera in Contant Region and how to capture the image in contact region. kindly clarify it....
The view you navigate into a region will contain all the functionality of your camera. Nothing changes. Just add your camera code and then navigate to that view.
Hi I am writing usercontrol in wpf in existing application which is winform (migrating to wpf) Can I use prism for usercontrol only .
It's possible, but not recommended.
@@BrianLagunas Thanks
how to call button or textbox in viewmodel?
Can you elaborate on what you mean?
@@BrianLagunas How to call a button out in viewModel?
for example
:
Button button = .... ( taken from View ) ;
button.background = Brushes.Red;
=> i using prism
MVVM
You would create properties in your VM and bind those to the button properties.
you mean:
Yes like that. For colors you may need to use a converter on your bidding to convert your property into a brush type
What if I need to send a message from a non-module view (In the shell app), to a module? How would I get the correct eventaggregator?
There is only one IEventAggregator in Prism. You'll always get the right one.
@@BrianLagunas Oh okay, so if I instantiate a new EventAggregator, I still get the same instance?
@@themrfj No, you can't instantiate it. It must come from the container. So inject the EA into your View's ctor, and use that one.
@@BrianLagunas Hmm, I think I might just stick to events through a shared library. My project doesn't really allow for the viewmodels in my shell project to inherit from a Prism class.
@@themrfj your VMs don't have to inherit from any Prism class.
I'm trying to do this but from a views code behind to the View Model and i keep get a null reference exception on start. Help? please...
If you are asking for the EventAggregator in the View's ctor, then make sure the container is creating your view.
You could be a Udemy star :)
One question to help me choose: what's the advantage of EventAggregator vs MessagingCenter?
I am actually a Pluralsight author :). The EventAggregator is a much better implementation of the MessagingCenter. From the API usage, to being able to filter events, and controlling which thread the events are invoked on. Simply put, the IEventAggregator is much better than the MessagingCenter
Amazing
Thanks for watching
My dude, it's 94 seconds into this video before you even start talking about the subject matter.
Please stop this madness. This video does not need to be padded like that.
Yeah I learned my lesson when I made these videos, and if you check out my latest videos, you'll see that I don't do this anymore. I was basically copying my Pluralsight course introduction styles. I learn with each video I do. Thanks for the feedback though.
@@BrianLagunas For sure! Lovin' the modern videos :)