Build a Messaging Platform with Xamarin Forms in the Easiest Way

Artboard-1024x757

To make uninterrupted connectivity between the individuals, though they are living near of far away from each other, a smart messaging platform plays a vital role. Hence believing in the same term, today we can found several messaging applications on the iTunes and play store which is getting too much appreciation from the users across the world through its flawless and smooth message sending and receiving functionality. Due to the per day increment in the popularity of such applications today a lot of developers are also looking to develop a multi-functional cross-platform messaging app, and in the same series, we are here to guide you with the simplest way of developing a finest messaging application with Xamarin.Forms.

As Xamarin is a cross-platform app development technology, that means an application which is developed under the Xamarin platform can be easily accessible on Android, iOS and Windows phone as well.

Mvvm Cross Messenger Plugin: The Trendy Way

For the beginning towards the development of a Xamarin.Forms messaging app, you need to work on the Mvvmcross Messanger Plugin. For this just creates a new message type, take it from Mvx Message on your project and publish it. Towards the sending of a message in a simpler way, the use of Mvvm messager is the best option.

Xamarin.Forms Messaging Center: The Odd Way

The messaging center of xamarin.form is like an earlier way to develop an app with a messaging option. It makes a developer to meet some odd features.

IMessanger: Improved Messaging Center

In a perfect messaging, the message and subscriber are like the focal points of the developer, and for the better appearance of a messaging in Xamarin.Forms, use this code:

public interface IMessage

{

}

public interface IMessenger

{

void Send<TMessage>(TMessage message, object sender = null)

where TMessage : IMessage;

 

void Subscribe<TMessage>(object subscriber, Action<object, TMessage> callback)

where TMessage : IMessage;

void Unsubscribe<TMessage>(object subscriber)

where TMessage : IMessage;

}

public class FormsMessenger : IMessenger

{

public void Send<TMessage>(TMessage message, object sender = null) where TMessage : IMessage

{

if (sender == null)

sender = new object();

MessagingCenter.Send<object, TMessage>(sender, typeof(TMessage).FullName, message);

}

public void Subscribe<TMessage>(object subscriber, Action<object, TMessage> callback) where TMessage : IMessage

{

MessagingCenter.Subscribe<object, TMessage>(subscriber, typeof(TMessage).FullName, callback, null);

}

public void Unsubscribe<TMessage>(object subscriber) where TMessage : IMessage

{

MessagingCenter.Unsubscribe<object, TMessage>(subscriber, typeof(TMessage).FullName);

}

}

Code to Define the Message Body

public class AlbumCreatedMessage : IMessage

{

public readonly Album Album;

public AlbumCreatedMessage(Album album)

{

Album = album;

}

}

Code for Sending a Message

var album = new Album

{

Id = Guid.NewGuid(),

Title = “Hello World”

};

// An instance of IMessenger

Messenger.Send(new AlbumCreatedMessage(album));

A Simple Code Line to Subscribe a Message

Messenger.Subscribe<AlbumCreatedMessage>(this, AlbumAdded);

private void AlbumAdded(object sender, AlbumCreatedMessage message)

{

// Do something

}

Use the Code, if You Want to Unsubscribe a Message

Messenger.Unsubscribe<AlbumCreatedMessage>(this);

Apply the code in your project and enjoy running it.

Xamarin Development Company
Visit here to view our portfolio: http://hire-xamarin-developer.com/Portfolio.php
Email: info@hire-xamarin-developer.com
Call:  USA – +1- (979)-599-0896,      +1-(979)-633-4727

by admin

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>