I am trying to use the TinyMessenger in my iPad application. The iPad application has few UIViewControllers. I would like to see the communication among these controllers via TinyMessenger. I understand the steps as
var messageHub = new TinyMessengerHub();
messageHub.Publish(new MyMessage());
messageHub.Subscribe<MyMessage>((m) => { MessageBox.Show("Message Received!"); });
And MyMessage is defined as below
public class MyMessage : ITinyMessage
{
/// <summary>
/// The sender of the message, or null if not supported by the message implementation.
/// </summary>
public object Sender { get; private set; }
}
Please advice if this the correct step to get this setup working. And I dont know where I should create the messagehub. I believe the messagehub has to be global so that it can be accessed by any UIViewController. Can I create messagehub in AppDelegate? If create the messagehub in AppDelegate, how do I access the messagehub from UIViewController1?
Appreciate any help.
You need to use this messenger hub in combination with an IoC container.
You need only 1 instance of the hub within your app, and using a container is the way to do this.
See the example from TinyIoC of setting up the container (which I believe you're using). You would basically call:
var hub = TinyIoCContainer.Current.Resolve<ITinyMessengerHub>();
//for subscribers
hub.Subscribe<YourMessage>(OnYourMessage);
//for publishers
hub.Publish(new YourMessage(this, "BOOYAH!"));
PS - if you are truly using TinyIoC, there is a #if TINYMESSENGER
preprocessor directive you can turn on to automatically register a hub for your application.