Тёмный

Prism - Using the Event Aggregator 

Brian Lagunas
Подписаться 18 тыс.
Просмотров 31 тыс.
50% 1

Do you need to communicate with different parts of your application which may, or may not, have a reference or knowledge of each other? In this video, I show you how to use Prism's IEventAggregator to communicate between loosely couples components of your application.
Follow me on Twitter
/ brianlagunas
Please visit this video sponsor
bit.ly/prism-infragistics
Help support Prism and become a Patron
/ prismlibrary

Опубликовано:

 

10 янв 2019

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 94   
@User_Masuri
@User_Masuri 3 года назад
Perfect video!! It makes me feel I don't need to search another video about EventAgregator in youtube anymore!
@BrianLagunas
@BrianLagunas 3 года назад
Wow, thank you so much
@DanielePrevato
@DanielePrevato 5 лет назад
Brian is an awesome teacher... he makes you understand everything
@BrianLagunas
@BrianLagunas 5 лет назад
Thank you for the kind words
@rahulmathew8713
@rahulmathew8713 3 года назад
@@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.
@rahulmathew8713
@rahulmathew8713 4 года назад
Awesome cool feature, solved a very complicated issue superfast after I saw this. Thank you so much Brian.
@BrianLagunas
@BrianLagunas 4 года назад
Thank you for watching. Be sure to share with your community
@SachidanandSuraj
@SachidanandSuraj 5 лет назад
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
@BrianLagunas
@BrianLagunas 4 года назад
Thank you so much for the kind words. I will try not to disappoint. Thanks for watching.
@randypenajimenez3893
@randypenajimenez3893 3 года назад
WPF is awesome, Brian is awesome, Prism is awesome. Prism makes everything so easy and clean.
@BrianLagunas
@BrianLagunas 3 года назад
Thank you very much for the kind words. You're awesome!
@nahuelmisc
@nahuelmisc 2 года назад
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!
@BrianLagunas
@BrianLagunas 2 года назад
Thanks for watching
@moifouss
@moifouss 5 лет назад
Thanks... that's really easy to understand. I want to see more videos about Prism.
@BrianLagunas
@BrianLagunas 5 лет назад
Be sure to subscribe to my channel so you are notified when new videos are published
@pheellomolokoli5016
@pheellomolokoli5016 2 года назад
This is great, simple explanation💯
@BrianLagunas
@BrianLagunas 2 года назад
Thanks for watching
@mgregory22
@mgregory22 Год назад
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!
@whatsupbudbud
@whatsupbudbud 5 лет назад
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.
@BrianLagunas
@BrianLagunas 5 лет назад
Thanks for watching. It is part of the official documentation. Take a look here prismlibrary.github.io/docs/event-aggregator.html
@draqula004
@draqula004 5 лет назад
Amazing
@BrianLagunas
@BrianLagunas 5 лет назад
Thanks for watching
@sometexthere4169
@sometexthere4169 5 лет назад
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!
@BrianLagunas
@BrianLagunas 5 лет назад
You can find docs here: prismlibrary.github.io/ and you can see samples here: github.com/PrismLibrary/Prism-Samples-Wpf
@proandy4448
@proandy4448 5 лет назад
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?
@BrianLagunas
@BrianLagunas 5 лет назад
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.
@tianhaozhang3019
@tianhaozhang3019 4 года назад
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.
@BrianLagunas
@BrianLagunas 4 года назад
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.
@CyleThomas
@CyleThomas 2 года назад
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?
@CyleThomas
@CyleThomas 2 года назад
I was using the wrong package of Prism. I was using PrismCore and not Prism for wpf. Hope this is useful for someone
@CyleThomas
@CyleThomas 2 года назад
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.
@BrianLagunas
@BrianLagunas 2 года назад
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
@sudhakar1184
@sudhakar1184 5 лет назад
Thank you Brian. Please provide a source code link to presented example.
@BrianLagunas
@BrianLagunas 5 лет назад
You can find all Prism WPF samples here: github.com/PrismLibrary/Prism-Samples-Wpf
@za-josoftware3213
@za-josoftware3213 4 года назад
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?
@BrianLagunas
@BrianLagunas 4 года назад
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.
@za-josoftware3213
@za-josoftware3213 4 года назад
@@BrianLagunas Wow, that was an incredibly quick response. I did it in the service class. Thanks, you are the best.
@InterRubke
@InterRubke 3 года назад
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.
@BrianLagunas
@BrianLagunas 3 года назад
So what's the problem exactly?
@joanesgonzaga675
@joanesgonzaga675 5 месяцев назад
Hi, Brian! What if I can't use Dependency Injection as you did? When I instantiate EventAggregator for subscriber it cames with message Null
@joanesgonzaga675
@joanesgonzaga675 5 месяцев назад
I have two windows services and I need to send a message from one to another (.Net Framework 4.7.2)
@AzgariusPWI
@AzgariusPWI 3 года назад
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?
@BrianLagunas
@BrianLagunas 3 года назад
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
@AzgariusPWI
@AzgariusPWI 3 года назад
@@BrianLagunas Ok, please check this out: github.com/Astralius/PrismEventAggregationProblem. I stripped it down to the parts that may matter.
@AzgariusPWI
@AzgariusPWI 3 года назад
@Brian Lagunas: Have you cloned it yet? :)
@BrianLagunas
@BrianLagunas 3 года назад
@@AzgariusPWI It worked for me 🤷‍♀️
@AzgariusPWI
@AzgariusPWI 3 года назад
Issue is now resolved on the linked github, with SO reference (for anyone who might have similar problem).
@cristianmerli5050
@cristianmerli5050 4 года назад
You could be a Udemy star :) One question to help me choose: what's the advantage of EventAggregator vs MessagingCenter?
@BrianLagunas
@BrianLagunas 4 года назад
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
@premkumars9944
@premkumars9944 4 года назад
Hi! Brain, I have doubt. How to open camera in Contant Region and how to capture the image in contact region. kindly clarify it....
@BrianLagunas
@BrianLagunas 4 года назад
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.
@alexalex-wj4pv
@alexalex-wj4pv 4 года назад
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?
@BrianLagunas
@BrianLagunas 4 года назад
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.
@lemoncrazedcamel
@lemoncrazedcamel 5 лет назад
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...
@BrianLagunas
@BrianLagunas 5 лет назад
If you are asking for the EventAggregator in the View's ctor, then make sure the container is creating your view.
@erikrepenning415
@erikrepenning415 4 года назад
When I try to do this I need to a parameterless contructor and I don't now why any help would be appreciated!
@BrianLagunas
@BrianLagunas 4 года назад
I have no idea. All I know is that you're doing something wrong :)
@taab84
@taab84 4 года назад
Amazing
@BrianLagunas
@BrianLagunas 4 года назад
Yes
@divge1
@divge1 5 лет назад
Hi I am writing usercontrol in wpf in existing application which is winform (migrating to wpf) Can I use prism for usercontrol only .
@BrianLagunas
@BrianLagunas 5 лет назад
It's possible, but not recommended.
@divge1
@divge1 5 лет назад
@@BrianLagunas Thanks
@lavanyadeshmukh2467
@lavanyadeshmukh2467 3 года назад
putting IEventAggregator in the constructor of the viewmodel is producing an error for me. The view simply doesn't initialize. Did I miss something?
@BrianLagunas
@BrianLagunas 3 года назад
If it's not working you definitely missed something.
@badboy13592
@badboy13592 2 года назад
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
@BrianLagunas
@BrianLagunas 2 года назад
Hard to say what your issue is. What is null? Seems that is the source of your problem.
@badboy13592
@badboy13592 2 года назад
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.
@BrianLagunas
@BrianLagunas 2 года назад
@@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.
@badboy13592
@badboy13592 2 года назад
@@BrianLagunas Thanks for reply. It worked but the constructor shall be IEA=EA()
@themrfj
@themrfj 4 года назад
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?
@BrianLagunas
@BrianLagunas 4 года назад
There is only one IEventAggregator in Prism. You'll always get the right one.
@themrfj
@themrfj 4 года назад
@@BrianLagunas Oh okay, so if I instantiate a new EventAggregator, I still get the same instance?
@BrianLagunas
@BrianLagunas 4 года назад
@@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.
@themrfj
@themrfj 4 года назад
@@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.
@BrianLagunas
@BrianLagunas 4 года назад
@@themrfj your VMs don't have to inherit from any Prism class.
@hauduong9045
@hauduong9045 2 года назад
how to call button or textbox in viewmodel?
@BrianLagunas
@BrianLagunas 2 года назад
Can you elaborate on what you mean?
@hauduong9045
@hauduong9045 2 года назад
@@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
@BrianLagunas 2 года назад
You would create properties in your VM and bind those to the button properties.
@hauduong9045
@hauduong9045 2 года назад
you mean:
@BrianLagunas
@BrianLagunas 2 года назад
Yes like that. For colors you may need to use a converter on your bidding to convert your property into a brush type
@DrLegitimate
@DrLegitimate 3 года назад
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.
@BrianLagunas
@BrianLagunas 3 года назад
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.
@DrLegitimate
@DrLegitimate 3 года назад
@@BrianLagunas For sure! Lovin' the modern videos :)
Далее
Prism - Using the ViewModelLocator
8:40
Просмотров 20 тыс.
Prism - Using Delegate Commands
11:31
Просмотров 36 тыс.
НУБ ИЩЕТ ЖЕНУ В GTA SAMP
22:34
Просмотров 363 тыс.
Is LEARNING WPF still WORTH it in 2023?
10:59
Просмотров 41 тыс.
Prism - Using Composite Commands
10:36
Просмотров 14 тыс.
MVVM Made Simple with Prism - Webinar
1:20:01
Просмотров 115 тыс.
Prism for Xamarin.Forms - Navigation Basics
15:09
Просмотров 22 тыс.
Blazor Component Events using EventCallback
5:14
Просмотров 13 тыс.
What is MVVM (Model-View-ViewModel) Pattern?
10:08
Просмотров 325 тыс.
5 Coding Projects (from beginner to advanced)
18:10
Просмотров 290 тыс.