Press ESC to close

Xamarin Forms Messaging Center

Hi friends,

In this article, we will talk about Messaging Center which is one of the most used features in Xamarin Forms. As the name of the Messaging Center implies, it is cross-page messaging. If you have had Native application development experience before, you can do it with Notification Center on iOS with Swift. We use the Messaging Center for many reasons. These; printing error message on the screen, performing the same operation on each page at the same time, sending news before 2 3 pages, data flow between pages etc. use cases. Very simple to use.

The Messaging Center has the part that receives a message and the part that sends the message. The part that receives the message must first mention it on that page. What it needs here is a value and the type of message that comes with this value is important when the message is sent. Let’s make an example. This example should show a message on the screen that a successful operation is successful.

First of all you need to register this message in the App class in order to display this message from all pages. Normally you can only register on one page, but you need to register in App.cs to show it on all pages. The way you register is actually very simple. Subscribe and specify the type of return. Then we tell you which page will be registered for this message and write which message should be registered. Then, if we get this message, we do what we do not.

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace XamMessagingCenter
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MessagingCenter.Subscribe<string>(this, "SuccessMessage", (obj) =>
              {
                  App.Current.MainPage.DisplayAlert("Success", obj, "OK");
              });

            MainPage = new MainPage();
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

After that, the message remains. Sending a message is a much simpler process. You only write what type of message you want to send and what message you send. Then you write the Key to which message to send.

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace XamMessagingCenter
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MessagingCenter.Subscribe<string>(this, "SuccessMessage", (obj) =>
              {
                  App.Current.MainPage.DisplayAlert("Success", obj, "OK");
              });

            MainPage = new MainPage();
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

Comments (1)

Leave a Reply

Your email address will not be published. Required fields are marked *