Xamarin.Forms - Create Message Center App

Introduction

This article demonstrates how to create Message Center application using C# and XAML in Xamarin Forms.

 

Let’s start,

Step 1

Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross-Platform >> Cross Patform App (Xamarin).

Select Cross-Platform App, then give Project Name and Project Location.

 

Step 2

Next, open Solution Explorer >> Project Name >> MainPage.xaml. After that, open the Design View of this page.

 

Step 3

We need to add three buttons and different click events. Next, we are  Binding Events Save to add Items Source in ListView.

 
 
XML Code
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:Messagecenters"  
  5.              x:Class="Messagecenters.MainPage">  
  6.      
  7.         <StackLayout Margin="0,20,0,0">  
  8.               
  9.             <StackLayout Orientation="Horizontal"   
  10.                          HorizontalOptions="CenterAndExpand">  
  11.                 <Button  Text="Sender"   
  12.                          Clicked="HandleSender_Clicked" />  
  13.                 <Button  Text="Reciver"   
  14.                          Clicked="HandleReceiver_Clicked" />  
  15.                 <Button  Text="Clear"   
  16.                          Clicked="HandleClear_Clicked" />  
  17.             </StackLayout>  
  18.   
  19.                 <ListView ItemsSource="{Binding Events}" />  
  20.               
  21.         </StackLayout>  
  22.      
  23. </ContentPage>  
  24.       
Step 4

Next Open Solution Explorer, Project Name, MainPage.xaml.cs and then click to Open C# Code.

 
 
C# Code
  1. using System;  
  2. using System.Collections.ObjectModel;  
  3. using Xamarin.Forms;  
  4.   
  5. namespace Messagecenters  
  6. {    public partial class MainPage : ContentPage  
  7.     {  
  8.         private string _tickContract = "tick";  
  9.         public ObservableCollection<string> Events { get; set; } = new ObservableCollection<string>();  
  10.         public MainPage()  
  11.         {  
  12.             InitializeComponent();   BindingContext = this;  
  13.         }  
  14.        void HandleSender_Clicked(object sender, System.EventArgs e)  
  15.         {  
  16.             MessagingCenter.Send(this, _tickContract, DateTime.Now);  
  17.         }  
  18.         void HandleReceiver_Clicked(object sender, EventArgs args)  
  19.         {  
  20.             MessagingCenter.Subscribe<MainPage, DateTime>(this, _tickContract, (s, a) => {  
  21.                 Events.Add($"Received message at {a.ToString()}");  
  22.             });  
  23.         }  
  24.         void HandleClear_Clicked(object sender, EventArgs args)  
  25.         {  
  26.             Events.Clear();  
  27.         }  
  28.     }  
  29.  }  
 Here, first we add namespace and then declare variable of String, after that value store using this variable, Go to on create () Observable Collection of given datatype. Then adding button events and some different conditions.

Create Receiver handle click Events: Events.Add($"Received message at {a.ToString()}");

 

Step 5 

Go to right click on solution explorer, Set StartUp Project, after that open Solution Project name Property pages. Select Multiple Startup projects, Start with Android and UWP platforms.

 
 
 

Step 6

Press F5 OR Build and Run the application, after that open Application Click Receiver button

And then click Sender button

OUTPIUT 1 

 
 
 OUTPUT 2 

Again continue same process, after that see the output adding two values on one ListView.
 
 
 
OUTPUT 3
 
 

Finally, we have successfully created a Xamarin Forms Message Center App.