Please give me some explaination about the following Microsoft.ToolKit.MVVM
-
I want to learn about the Messenger feature of Microsoft.ToolKit.MVVM. In the documentation, I found the following code:
// Create a message
public class LoggedInUserRequestMessage : RequestMessage
{
}// Register the receiver in a module
WeakReferenceMessenger.Default.Register(this, (r, m) =>
{
// Assume that "CurrentUser" is a private member in our viewmodel.
// As before, we're accessing it through the recipient passed as
// input to the handler, to avoid capturing "this" in the delegate.
m.Reply(r.CurrentUser);
});// Request the value from another module
User user = WeakReferenceMessenger.Default.Send();I don't know what this, r, and m are. Do we need to always inherit from RequestMessage? I need a simple example of how to use the Messenger feature. Please guide me.
-
I want to learn about the Messenger feature of Microsoft.ToolKit.MVVM. In the documentation, I found the following code:
// Create a message
public class LoggedInUserRequestMessage : RequestMessage
{
}// Register the receiver in a module
WeakReferenceMessenger.Default.Register(this, (r, m) =>
{
// Assume that "CurrentUser" is a private member in our viewmodel.
// As before, we're accessing it through the recipient passed as
// input to the handler, to avoid capturing "this" in the delegate.
m.Reply(r.CurrentUser);
});// Request the value from another module
User user = WeakReferenceMessenger.Default.Send();I don't know what this, r, and m are. Do we need to always inherit from RequestMessage? I need a simple example of how to use the Messenger feature. Please guide me.
this
is a keyword in C# which always represents the current instance of the current class - as such it s only ever available in non-static methods, becausestatic
code does not and cannot refer to an instance at all. It's like a car: "my car" is an instance, "your car" is a different instance. But "this car" could refer to either, and the question "what colour is this car?" will return a different value depending on which instance we are talking about. If we are driving in my car, then the answer would be "black" because "my car" is a black Mercedes. If your BMW is being driven then the answer might be "green".r
r
andm
are parameters which come from the invocation of the lambda: MVVM - The MVVM Light Messenger In-Depth | Microsoft Docs[^] is a good place to start."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!