How to Build a Native Xamarin.Forms Custom Modal Popup

Hello, friends. You might have noticed that when building Xamarin.Forms applications, you can only call extremely simple modal, popups, or dialog (these words will be used interchangeably). These popups are used only for very simple scenarios such as displaying information or allowing users to select a set of predefined options only. Where the need of building Xamarin.Forms Custom Modal Popups.

Other scenarios require more complex modals that allow the addition of entries, input views and other complex views. Xamarin.Forms does not provide any obvious way of doing these custom popup. At least not without additional external plugins, or tricks else not to my knowledge yet. Adding views to Xamarin.Forms pages is obvious and easy. But the Xamarin.Forms Alert does not provide a way to add views on it for customization.

This is not so ideal, since one may need to call custom popups to perform simple tasks. Like getting numbers, or text or any type of input from the user. And do so without much stress. What we will be doing in this tutorial is just that. Calling native custom modals and passing data back to the shared code while respecting the MVVM design pattern.

What we will be doing

To make everything short, we will go through all the steps to implement a custom native modal popup in xamarin.forms. And do so while respecting the MVVM pattern. Bellow is a bullet list of what will be done.

Let’s Dive In

The app which we will be building is a simple application which will permit users to create to do items and mark them as completed, the creation of these todo items will be done with dialogs, and when they are created, the underlying view will be populated with the newly created item. Let’s dive into it.

We will call the popup from each platform. We will use Xamarin.Forms dependency service. So create this interface in the shared code.

public interface ICallDialog
{
Task CallDialog(object viewModel);
}

Create a ViewModel for the main page. The part of the view model we are interested in is the command for calling the popup. Which is CreateCommand.

And also, we need to write the code to populate the Todo List. The list in the main view model, which is populated when an item is created on the modal popup. We subscribe and unsubscribe to the messages sent between the main view model and the modal popup’ s view model.

Let’s dive into the code to create the popups. We will do this for two platforms in this tutorial, which are UWP and Android.

Android Modal Popup

On android, to implement this with MVVM and ReactiveUI, install Reactiveui Android Support . Create a ReactiveDialogFragment this contains methods which we will leverage to bind the views to the view model’s properties we created earlier. This data binding is just a tricky one we will create using methods provided by the reactive dialog fragment. Here is the code to implement the layout for this dialog.

The code behind this dialog fragment contains mainly validation and binding views to the view model.

In the on create method of the dialog, we use the WhenAny method provided by the ReactiveDialogFragment to watch changes on the view and change the view model in consequence, that is some sort of data binding.

We are done

With this, running the sample on the platforms implemented should go smoothly. This is one solution to the problem of creating custom popups in Xamarin.Forms. There are other solutions to create modal popups in Xamarin.Forms. Some include using custom renderers and others include using tricks on Xaml layout to make the view feel like a modal popup. Just by searching on google, you could find these other solutions.But in my opinion, this method is the best.Since it makes call to native code. Also, the popups created link back to a view model in the shared code. Where data received is processed as it should be in a proper MVM application.

Don’t forget, here is the code for this tutorial, and if you think there is another better method for doing this, please share it in the comments. So that everyone reading this post can profit of it.

If you liked this post, or it was useful to you, please 👍 like it, share it on twitter, facebook or other social media… in case you want to get updated on any new useful post, follow me on twitter and like my page on facebook.