Level-up your Xamarin apps with HOW-TO Prism!? Part 2/24 min read

Let’s continue from where we left off.
So in the previous post (Part 1), we had a short overview of what is MVVM and why MVVM library when building Xamarin applications.
We covered navigation and events and left only with the last three topics to cover:
- ui dialogs
- dependency services
- messaging
UI dialogs
Let’s cover UI DIALOGS with some examples.
We all know what dialogs are. Most commonly they are used to display some kind of alert message.
To do this, we need to inject IPageDialogService.
i.e.:
public MainPageViewModel(INavigationService navigationService,
IPageDialogService dialogService)
{
_dialogService = dialogService;
}
and then call it like:
_dialogService.DisplayAlertAsync("Title of an alert", "Main message of an alert", "Yes - button", "No - button");
It’s really simple as that.
If we want to get the value of Alert dialog we do it like this:
var result = await _dialogService.DisplayAlertAsync("Title of an alert", "Main message of an alert", "Yes - button", "No - button");
and in the result we have only TRUE or FALSE value.
Besides DisplayAlertAsync we have and the option to show Action Sheets by using DisplayActionSheetAsync.
Its main advantage over Alert dialog is to provide us the ability o choose from more options. The syntax is quite similar as in the previous example:
_dialogService.DisplayActionSheetAsync("Title of an Action Sheet", "Cancel", null, "option 1", "option 2", "option 3", "option 4");
We can put as many options as we want here. To retrieve the result of an action sheet is the same as with alert dialog:
var resultAsString = await _dialogService.DisplayActionSheetAsync("Title of an Action Sheet", "Cancel", null, "option 1", "option 2", "option 3", "option 4");
Note, that result is of type string.
Dependency service
It’s a way to talk to the specific platform (i.e. Android or IOS), doing specific platform tasks. Since each platform has it’s own specifics, like accessing SQLite, Images, etc…
Note, that result is of type string.
Steps to create custom service:
1. create custom interface
public interface ICustomPrismService
{
void DoSomething(string value);
}
2. create Xamarin native Android or IOS project
3. implement custom interface into newly created project:
[assembly:Dependency(typeof(PrismService))]
namespace PrismCourseApp.Droid
{
public class CustomPrismService : ICustomPrismService
{
public void DoSomething(string value)
{
Debug.WriteLine("Value is:" + value);
}
}
}
hint – you need to add Dependency attribute
4. inject service where we want to use it, inside our forms application:
public CustomPageViewModel(IPrismService prismService)
{
_prismService = prismService;
}
5. call our newly created service within our ViewModel like this:
_prismService.DoSomething("Hello from forms!");
Pretty simple, ain’t it!?
Messaging
And finally, let’s look at messaging. It’s a great way to send data across the app without a lot of fuss. Using messaging is the best way to pass data around although you might send data using different methods.
But this is a way to go!
So what is messaging?
It’s an event bus system. With subscribers and publishers.
Publishers are the ones who send data(messages) triggered by user-interaction or similar and, you guessed it, Subscribers are the ones who receive data(messages) and execute upon receiving messages.
This way ViewModels or any other components don’t need to know anything about each other.
So let’s see how do we use messaging with Prism framework:
1. inject eventAggregator:
public class MainPageViewModel : BindableBase
{
public IEventAggregator _eventAggregator;
public MainPageViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
}
2. add a new class representing event and inherit accordingly:
public class EventPub : PubSubEvent<string>
{
}
* hence type between brackets “<string>” – it defines type of payload which event shall be using
3. subscribe to newly created event and show the data:
public class MainPageViewModel : BindableBase
{
public IEventAggregator _eventAggregator;
public MainPageViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<EventPub>().Subscribe(ChangeTitle);
}
}
As seen above, when subscribing to an event we define our function which will trigger upon receiving a message. ie.:
public void ChangeTitle(string payload) {
string Title = payload;
// do something now with this title...
}
4. publish event from some other view model, i.e:
public class SecondScreenViewModel : BindableBase
{
public IEventAggregator _eventAggregator;
public SecondPageViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<EventPub>().Publish("Title name has changed!");
}
}
5. unsubscribe after we have done all the execution of an event like this:
public class MainPageViewModel : BindableBase
{
public IEventAggregator _eventAggregator;
public MainPageViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<EventPub>().Unsubscribe(ChangeCityName);
}
}
Using Unsubscribe event we won’t receive any notifications about Title changes.
Fin
This sums up our Prism framework overview.
I hope you did get some valuable insights about why and when to use it.
Cheers.