Using Prism.Forms EventAggregator with Xamarin.Forms

If you are building a Xamarin.Forms app, it should be a no-brainer that you use MVVM pattern for your code base. It should also be a no-brainer that you give Prism.Forms a try. It makes development fast and code easy to maintain with its plethora of built-in services such for navigation, dependency injection, alerts, events, etc.

I recently added a sample to Prism’s samples library on how to use cross-platform EventAggregator using Prism.Forms in a Xamarin.Forms app.

In this sample, we will create custom events, pass custom payload in events, subscribe to events inside Xamarin.Forms app and on the native platforms, and publish events all using Prism.Forms EventAggregator.

Creating Events

To create an event, simply extend Prism.Events.PubSubEvent with a T payload.

public class IsFunChangedEvent : PubSubEvent<bool> { }

Usage

Publish

_eventAggregator.GetEvent<IsFunChangedEvent> ().Publish (true);

Subscribe

_eventAggregator.GetEvent<IsFunChangedEvent> ().Subscribe (IsFunChanged);

void IsFunChanged(bool arg)
{
   // Do something with the payload
}

Creating Events with Custom Payload

To create a custom payload for your event, simply extend System.EventArgs and set as payload for custom event,

public class NativeEventArgs : EventArgs
{
   public string Message { get; set; }
   public NativeEventArgs (string message)
   {
      Message = message;
   }
}

public class NativeEvent : PubSubEvent<NativeEventArgs> { }

Usage

Publish

_eventAggregator.GetEvent<NativeEvent> ().Publish (new NativeEventArgs("Xamarin.Forms"));

Subscribe

_eventAggregator.GetEvent<NativeEvent> ().Subscribe(OnNameChangedEvent);

void OnNameChangedEvent(NativeEventArgs args)
{
  Message = args.Message;
}

Subscribing To Events

As mentioned above, subscribing to events is quite simple inside Xamarin.Forms app.

_eventAggregator.GetEvent<IsFunChangedEvent> ().Subscribe (IsFunChanged);

void IsFunChanged(bool arg)
{
 // Do something with the payload
}

Platform Subscriptions

To subscribe to an event published in Xamarin.Forms app at the platform level, resolve the IEventAggregator from the Xamarin.Forms.App() instance before loading it.

iOS

After initializing the Xamarin.Forms app in FinishedLaunching() method of AppDelegate.cs,

var application = new App (new iOSInitializer ());
var ea = application.Container.Resolve<IEventAggregator> ().GetEvent<NativeEvent> ().Subscribe(OnNameChangedEvent);
LoadApplication (application);

Android

After initializing the Xamarin.Forms app in OnCreate() method of MainActivity.cs,

var application = new App (new AndroidInitializer ());
var ea = application.Container.Resolve<IEventAggregator> ().GetEvent<NativeEvent> ().Subscribe (OnNameChangedEvent);
LoadApplication (application);

UWP

After initializing the Xamarin.Forms app in OnLaunched() method of native App.xaml.cs, subscribe in the constructor of MainPage.xaml.cs,

var application = new UsingEventAggregator.App(new UwpInitialer());
var ea = application.Container.Resolve<IEventAggregator> ().GetEvent<NativeEvent> ().Subscribe(OnNativeEvent);
LoadApplication(application);

You can find the complete working sample here: https://github.com/PrismLibrary/Prism-Samples-Forms/tree/master/UsingEventAggregator

Enjoy!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.