Prism - Using the Event Aggregator

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 ธ.ค. 2024

ความคิดเห็น • 94

  • @User_Masuri
    @User_Masuri 4 ปีที่แล้ว +5

    Perfect video!! It makes me feel I don't need to search another video about EventAgregator in youtube anymore!

  • @randypenajimenez3893
    @randypenajimenez3893 4 ปีที่แล้ว +2

    WPF is awesome, Brian is awesome, Prism is awesome. Prism makes everything so easy and clean.

    • @BrianLagunas
      @BrianLagunas  4 ปีที่แล้ว +1

      Thank you very much for the kind words. You're awesome!

  • @DanielePrevato
    @DanielePrevato 5 ปีที่แล้ว +11

    Brian is an awesome teacher... he makes you understand everything

    • @BrianLagunas
      @BrianLagunas  5 ปีที่แล้ว

      Thank you for the kind words

    • @rahulmathew8713
      @rahulmathew8713 4 ปีที่แล้ว +1

      @@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.

  • @SachidanandSuraj
    @SachidanandSuraj 5 ปีที่แล้ว +3

    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.

  • @rahulmathew8713
    @rahulmathew8713 4 ปีที่แล้ว +1

    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

  • @nahuelmisc
    @nahuelmisc 3 ปีที่แล้ว +1

    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!

  • @mgregory22
    @mgregory22 2 ปีที่แล้ว

    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!

  • @pheellomolokoli5016
    @pheellomolokoli5016 2 ปีที่แล้ว +1

    This is great, simple explanation💯

  • @whatsupbudbud
    @whatsupbudbud 5 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      Thanks for watching. It is part of the official documentation. Take a look here prismlibrary.github.io/docs/event-aggregator.html

  • @moifouss
    @moifouss 6 ปีที่แล้ว +5

    Thanks... that's really easy to understand. I want to see more videos about Prism.

    • @BrianLagunas
      @BrianLagunas  6 ปีที่แล้ว +1

      Be sure to subscribe to my channel so you are notified when new videos are published

  • @CyleThomas
    @CyleThomas 2 ปีที่แล้ว +1

    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

  • @sometexthere4169
    @sometexthere4169 5 ปีที่แล้ว +2

    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 ปีที่แล้ว +1

      You can find docs here: prismlibrary.github.io/ and you can see samples here: github.com/PrismLibrary/Prism-Samples-Wpf

  • @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

  • @za-josoftware3213
    @za-josoftware3213 5 ปีที่แล้ว +1

    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  5 ปีที่แล้ว +1

      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 5 ปีที่แล้ว +1

      @@BrianLagunas Wow, that was an incredibly quick response. I did it in the service class. Thanks, you are the best.

  • @joanesgonzaga675
    @joanesgonzaga675 11 หลายเดือนก่อน

    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 11 หลายเดือนก่อน

      I have two windows services and I need to send a message from one to another (.Net Framework 4.7.2)

  • @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.

  • @AzgariusPWI
    @AzgariusPWI 4 ปีที่แล้ว +1

    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  4 ปีที่แล้ว +1

      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 4 ปีที่แล้ว

      @@BrianLagunas Ok, please check this out: github.com/Astralius/PrismEventAggregationProblem. I stripped it down to the parts that may matter.

    • @AzgariusPWI
      @AzgariusPWI 4 ปีที่แล้ว

      @Brian Lagunas: Have you cloned it yet? :)

    • @BrianLagunas
      @BrianLagunas  4 ปีที่แล้ว

      @@AzgariusPWI It worked for me 🤷‍♀️

    • @AzgariusPWI
      @AzgariusPWI 4 ปีที่แล้ว

      Issue is now resolved on the linked github, with SO reference (for anyone who might have similar problem).

  • @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()

  • @InterRubke
    @InterRubke 4 ปีที่แล้ว

    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  4 ปีที่แล้ว

      So what's the problem exactly?

  • @lavanyadeshmukh2467
    @lavanyadeshmukh2467 4 ปีที่แล้ว

    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  4 ปีที่แล้ว

      If it's not working you definitely missed something.

  • @proandy4448
    @proandy4448 6 ปีที่แล้ว

    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  6 ปีที่แล้ว +1

      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.

  • @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.

  • @premkumars9944
    @premkumars9944 5 ปีที่แล้ว

    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  5 ปีที่แล้ว

      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.

  • @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 :)

  • @sudhakar1184
    @sudhakar1184 6 ปีที่แล้ว

    Thank you Brian. Please provide a source code link to presented example.

    • @BrianLagunas
      @BrianLagunas  6 ปีที่แล้ว +1

      You can find all Prism WPF samples here: github.com/PrismLibrary/Prism-Samples-Wpf

  • @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 ปีที่แล้ว +1

      If you are asking for the EventAggregator in the View's ctor, then make sure the container is creating your view.

  • @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 ปีที่แล้ว +1

      It's possible, but not recommended.

    • @divge1
      @divge1 5 ปีที่แล้ว

      @@BrianLagunas Thanks

  • @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.

  • @taab84
    @taab84 4 ปีที่แล้ว

    Amazing

  • @cristianmerli5050
    @cristianmerli5050 4 ปีที่แล้ว +1

    You could be a Udemy star :)
    One question to help me choose: what's the advantage of EventAggregator vs MessagingCenter?

    • @BrianLagunas
      @BrianLagunas  4 ปีที่แล้ว +1

      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

  • @hauduong9045
    @hauduong9045 3 ปีที่แล้ว

    how to call button or textbox in viewmodel?

    • @BrianLagunas
      @BrianLagunas  3 ปีที่แล้ว

      Can you elaborate on what you mean?

    • @hauduong9045
      @hauduong9045 3 ปีที่แล้ว

      @@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  3 ปีที่แล้ว

      You would create properties in your VM and bind those to the button properties.

    • @hauduong9045
      @hauduong9045 3 ปีที่แล้ว

      you mean:

    • @BrianLagunas
      @BrianLagunas  3 ปีที่แล้ว

      Yes like that. For colors you may need to use a converter on your bidding to convert your property into a brush type

  • @draqula004
    @draqula004 5 ปีที่แล้ว +1

    Amazing

  • @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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@BrianLagunas For sure! Lovin' the modern videos :)