Skip to content

Instantly share code, notes, and snippets.

@juucustodio
Created November 25, 2018 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juucustodio/38148a50988626623ac06579359bc4df to your computer and use it in GitHub Desktop.
Save juucustodio/38148a50988626623ac06579359bc4df to your computer and use it in GitHub Desktop.
Example of how to use MessagingCenter in Xamarin.Forms applications. - https://julianocustodio.com/messagingcenter
using Android.App;
using Android.Widget;
using Android.Content.PM;
using Android.OS;
using Xamarin.Forms;
namespace DemoMessagingCenter.Droid
{
[Activity(Label = "DemoMessagingCenter", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
MessagingCenter.Subscribe<Message>(this, "AddItem", message => {
ShowAlert("Item adicionado");
});
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
public void ShowAlert(string message)
{
Toast.MakeText(ApplicationContext, message, ToastLength.Long).Show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment