diff --git a/Microsoft.Toolkit.Mvvm/Messaging/MessengerExtensions.Default.cs b/Microsoft.Toolkit.Mvvm/Messaging/MessengerExtensions.Default.cs new file mode 100644 index 00000000..0809c0fd --- /dev/null +++ b/Microsoft.Toolkit.Mvvm/Messaging/MessengerExtensions.Default.cs @@ -0,0 +1,107 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics.Contracts; + +namespace Microsoft.Toolkit.Mvvm.Messaging +{ + /// + /// Extensions for the type. + /// + public static partial class MessengerExtensions + { + /// + /// Checks whether or not a given recipient has already been registered for a message. + /// + /// The type of message to check for the given recipient. + /// The instance to use to check the registration. + /// The target recipient to check the registration for. + /// Whether or not has already been registered for the specified message. + /// This method will use the default channel to check for the requested registration. + [Pure] + public static bool IsRegistered(this IMessenger messenger, object recipient) + where TMessage : class + { + return messenger.IsRegistered(recipient, default); + } + + /// + /// Registers a recipient for a given type of message. + /// + /// The type of message to receive. + /// The instance to use to register the recipient. + /// The recipient that will receive the messages. + /// The to invoke when a message is received. + /// Thrown when trying to register the same message twice. + /// This method will use the default channel to perform the requested registration. + public static void Register(this IMessenger messenger, object recipient, Action action) + where TMessage : class + { + messenger.Register(recipient, default(Unit), action); + } + + /// + /// Unregisters a recipient from messages of a given type. + /// + /// The type of message to stop receiving. + /// The instance to use to unregister the recipient. + /// The recipient to unregister. + /// + /// This method will unregister the target recipient only from the default channel. + /// If the recipient has no registered handler, this method does nothing. + /// + public static void Unregister(this IMessenger messenger, object recipient) + where TMessage : class + { + messenger.Unregister(recipient, default); + } + + /// + /// Sends a message of the specified type to all registered recipients. + /// + /// The type of message to send. + /// The instance to use to send the message. + /// + /// This method is a shorthand for when the + /// message type exposes a parameterless constructor: it will automatically create + /// a new instance and send that to its recipients. + /// + public static void Send(this IMessenger messenger) + where TMessage : class, new() + { + messenger.Send(new TMessage(), default(Unit)); + } + + /// + /// Sends a message of the specified type to all registered recipients. + /// + /// The type of message to send. + /// The instance to use to send the message. + /// The message to send. + public static void Send(this IMessenger messenger, TMessage message) + where TMessage : class + { + messenger.Send(message, default(Unit)); + } + + /// + /// Sends a message of the specified type to all registered recipients. + /// + /// The type of message to send. + /// The type of token to identify what channel to use to send the message. + /// The instance to use to send the message. + /// The token indicating what channel to use. + /// + /// This method will automatically create a new instance + /// just like , and then send it to the right recipients. + /// + public static void Send(this IMessenger messenger, TToken token) + where TMessage : class, new() + where TToken : IEquatable + { + messenger.Send(new TMessage(), token); + } + } +}