A Better Pub/Sub Messenger for .NET Applications with WeakReferenceMessenger

preview_player
Показать описание
Sometimes you need to pass pieces of data to and from different parts of your application. This may be for communication between viewmodels or perhaps from your app startup class passing important information. With the .NET Community Toolkit you have a simple cross-platform way of sending and receiving messages no matter what type of app your are building. You can use it with .NET MAUI, WinUI, WPF, WinForms, ASP.NET Core, or just about any other .NET application!

Join this channel to get access to perks:

Follow:

Disclaimer: This channel, videos, and streams are created in my spare time and are a product of me... James Montemagno! They are NOT officially affiliated or endorsed by Microsoft (my employer) in any way. Opinions and views are my own.

Chapters:
00:00 - Why you need a messenger for apps
01:30 - App and code overview
03:40 - Adding CommunityToolkit.Mvvm
04:00 - Creating a message to send
06:05 - Subscribing to messages
10:00 - Sending messages
13:00 - Cleaner implementation with IRecipient
15:05 - Wrap-up

My Setup:

#dotnet #dotnetmaui #aspnetcore
Рекомендации по теме
Комментарии
Автор

Great video! You really make it easy to understand how these things work. I was looking for how to use the old 'MessagingCenter' saw that it was deprecated, and then was like, "What the heck is this WeakReferenceMessenger??". Makes total sense now, thanks!

thomas.carney
Автор

I learn a lot from your videos. I would like to see a video about background service with .NetMaui

ricardocosta
Автор

Wow, good to see that Microsoft embraced this essential feature.
I am looking forward to trying it out in a Blazor app, where the communication between the different components is a key feature.

tegusolutions
Автор

Hey James, this is a really good tutorial !

I followed your beginner series and have been having hard time to implement this myself.
I think others may find this helpful, so it would be cool if you add it to the playlist as a final view

Thanks for the video :)

Henok-qnnc
Автор

Great stuff! 👍🏻 Looking forward to more Toolkit 8.0.0 goodies!

leszekjezierski
Автор

Excellent James. Assume I could use this in a service, such as to listen to signalR pipeline messages. Then blast out messages within the app.

BrendanAlexander
Автор

As always, thank you! This has been supremely helpful!

jeff-uqsz
Автор

Wow! Wow! Wow! This is exactly what I'm looking for! Many thanks!

alexpinkerton
Автор

Enjoying all your content: Love the Breakdown and Usage!
I need a way of consistently using these toolkits in everyday code :D

Dazza_Doo
Автор

Thanks for the video, much better than the documentation!

calinmh
Автор

Another great video. Thanks. Have you thought any more about a video demystifying Maui's csproj? Everytime anything gets added, another 20 lines show up in there! include, remove, update, etc LOL

billymartin
Автор

Pretty cool to see this used. I think I would still prefer the reactive extensions package for some use cases.

CantonGregory
Автор

I was scrambling all over the internet the last few days trying to understand how to actually use this feature on the toolkit... managed to understand a bit of the official documentation and its such a great feature, this video just helped me to further refine my usage of it.

question though, I personally prefer having my commands in their own separate classes. Is there any way you'd recommend for me to go about it while making use of the mvvm toolkit?

masou
Автор

I am using it in WPF currently and it's really nice to decouple modules. 👍
Question: What's the reason to ensure the message is handled by the main thread?

Mr.Thompson
Автор

Great video! A small question: What is the added value of using this ValueChangedMessage type because you can basically send any object of any type as a message.

keigezellig
Автор

What's the difference compared to Mediatr and why should I use Messenger instead? Thanks in advance!

lucademarco
Автор

Great video. Thanks! Is there a way to be a recipient of two or three different messages?

M-fq
Автор

Does not Maui already have MessagingCenter that does the same job?

majesticjester
Автор

Question:
For this to work do you need to make sure that A) when using a DI tool that your Weak/Strong reference messenger is a singleton and B) that the recipient view model is also some form of singleton?

I know in my early days working with this pattern I would wonder why things weren't working and it was due to the DI tool creating a new instance of things when it would resolve the view model type. Can't send messages between two things if those two things have two different instances of the messenger. I'm sure the "Default" property is static and so it wouldn't be noticed. Maybe the view models could still suffer the same fate if not registered in DI properly?

logank.
Автор

Thank you for getting to the point right away. Liked and subscribed!

AndrewRiebe