You can also pass arguments with a message to work with.
We will use the classed from our previous example and extend them. In the receiving part, right behind the Subscribe
method call add the type of the argument you are expecting. Also make sure you also declare the arguments in the handler signature.
public class FooMessaging
{
public string Greeting { get; set; }
public FooMessaging()
{
MessagingCenter.Subscribe<MainPage, string> (this, "Hi", (sender, arg) => {
this.Greeting = arg;
});
}
}
When sending a message, make sure to include the argument value. Also, here you add the type right behind the Send
method and add the argument value.
public class MainPage : Page
{
private void OnButtonClick(object sender, EventArgs args)
{
MessagingCenter.Send<MainPage, string> (this, "Hi", "Hi there!");
}
}
In this example a simple string is used, but you can also use any other type of (complex) objects.