[Feature] Behaviors for WeakReferenceMessenger and StrongReferenceMessenger #239
Replies: 5 comments
-
Hello, 'Arlodotexe! Thanks for submitting a new feature request. I've automatically added a vote 👍 reaction to help get things started. Other community members can vote to help us prioritize this feature in the future! |
Beta Was this translation helpful? Give feedback.
-
This is an interesting proposal. As I mentioned on Discord, I think before discussing this in detail, we should reason about where would this go. Given this requires whatever package contains it to have a reference to the MVVM Toolkit, this automatically means it's a complete non-starter for any of the existing packages in the Windows Community Toolkit. As such, we'd need to create a new package to contain this code, such as
|
Beta Was this translation helpful? Give feedback.
-
Yeah, we wouldn't want the Behaviors package itself to also pull in the MVVM library, I think. I mean it does pull in the Animations package, and soon WinUI itself, but MVVM is generally isolated. I really wish we could have dependencies only be pulled in if certain code is used... would make things so much easier. I mean in the new I do like this idea though. Definitely seems like a good thing to at least experiment with in 🧪 Labs... 😉 |
Beta Was this translation helpful? Give feedback.
-
It may be worth considering scrapping the "Filters" idea and creating a separate, generic conditional trigger wrapper. |
Beta Was this translation helpful? Give feedback.
-
FYI @Sergio0694 this may also play into what you've been playing with lately too as a separate or coordinated lab idea. |
Beta Was this translation helpful? Give feedback.
-
Problem
The MVVM Toolkit has a StrongReferenceMessenger and a WeakReferenceMessenger, used for relaying messages via the messenger pattern.
These APIs are extremely handy for passing messages around to decoupled parts of the app, but they require code-behind to send and receive messages.
In scenarios where the user may prefer or be forced to write only XAML (such as retemplating a control), it becomes cumbersome to send and receive messages with the default Messenger.
Solution
It would useful to developers if they had the ability to send and receive messages for
WeakReferenceMessenger
andStrongReferenceMessenger
via Behavior actions and triggers in XAML.tl;dr
XAML has Behaviors.
Behaviors are like bacon.
Messenger needs bacon.
Examples
Sending a message
The user specifies a
SendMessageAction
, changes the messenger toWeak
orStrong
as needed, and provides the model to send.Receiving a message
Unconditional triggering
It would be easy to just invoke an action when a message of a certain type is emitted.
Conditional triggering
However, unconditional triggering doesn't fit most user scenarios. We need the ability to conditionally execute the behavior based on data in the model, since the action performed often depends on what data is in the message.
There's a few options here. Which approach is best is open to discussion. (I'm personally a fan of the filters approach.)
Using filters declared in XAML
This is similar to DataTriggerBehavior, with a few key differences
&&
(and
).||
(or
) by declaringOptional="True"
.Using a converter
This option has the most flexibility but requires C# code and a questionable usage of converters.
Additional info
The messenger pattern itself closely resembles a pub-sub pattern, and so all decoupling benefits of the pub-sub pattern come with it, no matter the context, and even outside of ViewModels.
For example
The messenger pattern is fantastic for general app navigation. This behavior would enable scenarios where navigation between pages is done 100% in XAML.
Help us help you
Yes, I'd like to be assigned to work on this item.
Beta Was this translation helpful? Give feedback.
All reactions